2 #include "OCCViewer_ViewModel.h"
3 #include "OCCViewer_ViewWindow.h"
4 #include "OCCViewer_VService.h"
5 #include "OCCViewer_ViewPort3d.h"
7 #include "SUIT_ViewWindow.h"
8 #include "SUIT_Desktop.h"
9 #include "SUIT_Session.h"
12 #include <qapplication.h>
13 #include <qcolordialog.h>
15 #include <qpopupmenu.h>
17 #include <AIS_Axis.hxx>
18 #include <AIS_Drawer.hxx>
19 #include <AIS_ListIteratorOfListOfInteractive.hxx>
21 #include <Geom_Axis2Placement.hxx>
22 #include <Prs3d_DatumAspect.hxx>
23 #include <Prs3d_LineAspect.hxx>
25 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron )
27 myBgColor( Qt::black )
29 // init CasCade viewers
30 myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
31 V3d_XposYnegZpos, true, true );
35 myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
36 V3d_XposYnegZpos, true, true );
37 myV3dCollector->Init();
40 myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
42 myAISContext->SelectionColor( Quantity_NOC_WHITE );
44 // display isoline on planar faces (box for ex.)
45 myAISContext->IsoOnPlane( true );
49 /* create trihedron */
50 if( DisplayTrihedron )
52 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
53 myTrihedron = new AIS_Trihedron(anAxis);
54 myTrihedron->SetInfiniteState( Standard_True );
56 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
57 //myTrihedron->SetColor( Col );
58 myTrihedron->SetArrowColor( Col.Name() );
59 myTrihedron->SetSize(100);
60 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
61 if (drawer->HasDatumAspect()) {
62 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
63 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
64 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
65 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
68 myAISContext->Display(myTrihedron);
69 myAISContext->Deactivate(myTrihedron);
73 mySelectionEnabled = true;
74 myMultiSelectionEnabled = true;
78 OCCViewer_Viewer::~OCCViewer_Viewer()
82 QColor OCCViewer_Viewer::backgroundColor() const
87 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
93 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
98 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
100 vp3d->setBackgroundColor( myBgColor );
105 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
107 OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
112 //*********************************************************************
113 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
115 SUIT_ViewModel::setViewManager(theViewManager);
116 if (theViewManager) {
117 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
118 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
120 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
121 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
123 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
124 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
129 //*********************************************************************
130 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
132 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
136 //*********************************************************************
137 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
139 if (!mySelectionEnabled) return;
140 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
142 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
143 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
147 //*********************************************************************
148 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
150 if (!mySelectionEnabled) return;
151 if (theEvent->button() != Qt::LeftButton) return;
152 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
155 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
156 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
157 bool aHasShift = (theEvent->state() & Qt::ShiftButton);
159 if (myStartPnt == myEndPnt)
161 if (aHasShift && myMultiSelectionEnabled)
162 myAISContext->ShiftSelect();
164 myAISContext->Select();
168 if (aHasShift && myMultiSelectionEnabled)
169 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
170 myEndPnt.x(), myEndPnt.y(),
171 aView->getViewPort()->getView(), Standard_False );
173 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
174 myEndPnt.x(), myEndPnt.y(),
175 aView->getViewPort()->getView(), Standard_False );
177 int Nb = myAISContext->NbSelected();
178 if( Nb>1 && !myMultiSelectionEnabled )
180 myAISContext->InitSelected();
181 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
182 if( !anOwner.IsNull() )
184 myAISContext->ClearSelected( Standard_False );
185 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
189 myAISContext->UpdateCurrentViewer();
191 emit selectionChanged();
195 //*********************************************************************
196 void OCCViewer_Viewer::enableSelection(bool isEnabled)
198 mySelectionEnabled = isEnabled;
199 //!! To be done for view windows
202 //*********************************************************************
203 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
205 myMultiSelectionEnabled = isEnable;
206 //!! To be done for view windows
209 //*********************************************************************
210 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
212 thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
213 thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
215 thePopup->insertSeparator();
217 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
218 if ( aView && !aView->getToolBar()->isVisible() )
219 thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
222 void OCCViewer_Viewer::onDumpView()
224 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
229 //*********************************************************************
230 void OCCViewer_Viewer::onChangeBgColor()
232 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
235 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
238 QColor aColorActive = aViewPort3d->backgroundColor();
240 QColor selColor = QColorDialog::getColor( aColorActive, aView);
241 if ( selColor.isValid() )
242 aViewPort3d->setBackgroundColor(selColor);
245 //*********************************************************************
246 void OCCViewer_Viewer::onShowToolbar() {
247 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
249 aView->getToolBar()->show();
252 //*********************************************************************
253 void OCCViewer_Viewer::update()
255 if (!myV3dViewer.IsNull())
256 myV3dViewer->Update();
259 //*********************************************************************
260 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
263 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
264 theList.Append(myAISContext->SelectedInteractive());
267 //*********************************************************************
268 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
270 AIS_ListIteratorOfListOfInteractive aIt;
271 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
272 myAISContext->SetSelected(aIt.Value(), false);
273 myAISContext->UpdateCurrentViewer();
276 //*********************************************************************
277 void OCCViewer_Viewer::performSelectionChanged()
279 emit selectionChanged();
282 //****************************************************************
284 void OCCViewer_Viewer::onClearViewAspects()
289 //****************************************************************
291 void OCCViewer_Viewer::clearViewAspects()
293 myViewAspects.clear();
296 //****************************************************************
298 const viewAspectList& OCCViewer_Viewer::getViewAspects()
300 return myViewAspects;
303 //****************************************************************
305 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
307 myViewAspects.append( aParams );
310 //****************************************************************
312 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
314 myViewAspects = aViewList;
317 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
318 bool hilight, bool update )
320 bool isInLocal = myAISContext->HasOpenedContext();
324 if ( hilight && !myAISContext->IsSelected( obj ) )
325 myAISContext->AddOrRemoveCurrentObject( obj, false );
326 else if ( !hilight && myAISContext->IsSelected( obj ) )
327 myAISContext->AddOrRemoveCurrentObject( obj, false );
331 myV3dViewer->Redraw();
336 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
338 if ( myAISContext->HasOpenedContext() )
339 myAISContext->ClearSelected( updateviewer );
341 myAISContext->ClearCurrents( updateviewer );
345 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
348 AIS_ListOfInteractive List;
349 myAISContext->DisplayedObjects(List);
353 AIS_ListOfInteractive List1;
354 myAISContext->ObjectsInCollector(List1);
358 AIS_ListIteratorOfListOfInteractive ite(List);
359 for ( ; ite.More(); ite.Next() )
360 if( ite.Value()==obj )
366 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
368 return myAISContext->IsDisplayed( obj );
371 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
377 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
378 color.green() / 255.,
381 obj->SetColor( CSFColor );
385 myV3dViewer->Update();
388 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
389 int mode, bool update )
391 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
393 myV3dViewer->Update();
396 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
397 float trans, bool update )
399 myAISContext->SetTransparency( obj, trans, false );
400 myAISContext->Redisplay( obj, Standard_False, Standard_True );
402 myV3dViewer->Update();
405 //****************************************************************
406 void OCCViewer_Viewer::toggleTrihedron()
408 setTrihedronShown( !isTrihedronVisible() );
411 bool OCCViewer_Viewer::isTrihedronVisible() const
413 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
416 void OCCViewer_Viewer::setTrihedronShown( const bool on )
418 if ( myTrihedron.IsNull() )
422 myAISContext->Display( myTrihedron );
424 myAISContext->Erase( myTrihedron );
427 int OCCViewer_Viewer::trihedronSize() const
430 if ( !myTrihedron.IsNull() )
431 sz = (int)myTrihedron->Size();
435 void OCCViewer_Viewer::setTrihedronSize( const int sz )
437 if ( !myTrihedron.IsNull() )
438 myTrihedron->SetSize( sz );
441 void OCCViewer_Viewer::setIsos( const int u, const int v )
443 Handle(AIS_InteractiveContext) ic = getAISContext();
447 ic->SetIsoNumber( u, AIS_TOI_IsoU );
448 ic->SetIsoNumber( v, AIS_TOI_IsoV );
451 void OCCViewer_Viewer::isos( int& u, int& v ) const
453 Handle(AIS_InteractiveContext) ic = getAISContext();
456 u = ic->IsoNumber( AIS_TOI_IsoU );
457 v = ic->IsoNumber( AIS_TOI_IsoV );