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 // display isoline on planar faces (box for ex.)
43 myAISContext->IsoOnPlane( true );
47 /* create trihedron */
48 if( DisplayTrihedron )
50 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
51 myTrihedron = new AIS_Trihedron(anAxis);
52 myTrihedron->SetInfiniteState( Standard_True );
54 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
55 //myTrihedron->SetColor( Col );
56 myTrihedron->SetArrowColor( Col.Name() );
57 myTrihedron->SetSize(100);
58 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
59 if (drawer->HasDatumAspect()) {
60 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
61 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
62 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
63 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
66 myAISContext->Display(myTrihedron);
67 myAISContext->Deactivate(myTrihedron);
71 mySelectionEnabled = true;
72 myMultiSelectionEnabled = true;
76 OCCViewer_Viewer::~OCCViewer_Viewer()
80 QColor OCCViewer_Viewer::backgroundColor() const
85 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
91 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
96 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
98 vp3d->setBackgroundColor( myBgColor );
103 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
105 OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
110 //*********************************************************************
111 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
113 SUIT_ViewModel::setViewManager(theViewManager);
114 if (theViewManager) {
115 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
116 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
118 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
119 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
121 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
122 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
127 //*********************************************************************
128 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
130 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
134 //*********************************************************************
135 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
137 if (!mySelectionEnabled) return;
138 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
140 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
141 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
145 //*********************************************************************
146 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
148 if (!mySelectionEnabled) return;
149 if (theEvent->button() != Qt::LeftButton) return;
150 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
153 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
154 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
155 bool aHasShift = (theEvent->state() & Qt::ShiftButton);
157 if (myStartPnt == myEndPnt)
159 if (aHasShift && myMultiSelectionEnabled)
160 myAISContext->ShiftSelect();
162 myAISContext->Select();
166 if (aHasShift && myMultiSelectionEnabled)
167 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
168 myEndPnt.x(), myEndPnt.y(),
169 aView->getViewPort()->getView(), Standard_False );
171 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
172 myEndPnt.x(), myEndPnt.y(),
173 aView->getViewPort()->getView(), Standard_False );
175 int Nb = myAISContext->NbSelected();
176 if( Nb>1 && !myMultiSelectionEnabled )
178 myAISContext->InitSelected();
179 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
180 if( !anOwner.IsNull() )
182 myAISContext->ClearSelected( Standard_False );
183 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
187 myAISContext->UpdateCurrentViewer();
189 emit selectionChanged();
193 //*********************************************************************
194 void OCCViewer_Viewer::enableSelection(bool isEnabled)
196 mySelectionEnabled = isEnabled;
197 //!! To be done for view windows
200 //*********************************************************************
201 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
203 myMultiSelectionEnabled = isEnable;
204 //!! To be done for view windows
207 //*********************************************************************
208 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
210 thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
211 thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
213 thePopup->insertSeparator();
215 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
216 if ( aView && !aView->getToolBar()->isVisible() )
217 thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
220 void OCCViewer_Viewer::onDumpView()
222 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
227 //*********************************************************************
228 void OCCViewer_Viewer::onChangeBgColor()
230 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
233 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
236 QColor aColorActive = aViewPort3d->backgroundColor();
238 QColor selColor = QColorDialog::getColor( aColorActive, aView);
239 if ( selColor.isValid() )
240 aViewPort3d->setBackgroundColor(selColor);
243 //*********************************************************************
244 void OCCViewer_Viewer::onShowToolbar() {
245 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
247 aView->getToolBar()->show();
250 //*********************************************************************
251 void OCCViewer_Viewer::update()
253 if (!myV3dViewer.IsNull())
254 myV3dViewer->Update();
257 //*********************************************************************
258 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
261 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
262 theList.Append(myAISContext->SelectedInteractive());
265 //*********************************************************************
266 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
268 AIS_ListIteratorOfListOfInteractive aIt;
269 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
270 myAISContext->SetSelected(aIt.Value(), false);
271 myAISContext->UpdateCurrentViewer();
274 //*********************************************************************
275 void OCCViewer_Viewer::performSelectionChanged()
277 emit selectionChanged();
280 //****************************************************************
282 void OCCViewer_Viewer::onClearViewAspects()
287 //****************************************************************
289 void OCCViewer_Viewer::clearViewAspects()
291 myViewAspects.clear();
294 //****************************************************************
296 const viewAspectList& OCCViewer_Viewer::getViewAspects()
298 return myViewAspects;
301 //****************************************************************
303 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
305 myViewAspects.append( aParams );
308 //****************************************************************
310 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
312 myViewAspects = aViewList;
315 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
316 bool hilight, bool update )
318 bool isInLocal = myAISContext->HasOpenedContext();
322 if ( hilight && !myAISContext->IsSelected( obj ) )
323 myAISContext->AddOrRemoveCurrentObject( obj, false );
324 else if ( !hilight && myAISContext->IsSelected( obj ) )
325 myAISContext->AddOrRemoveCurrentObject( obj, false );
329 myV3dViewer->Redraw();
334 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
336 if ( myAISContext->HasOpenedContext() )
337 myAISContext->ClearSelected( updateviewer );
339 myAISContext->ClearCurrents( updateviewer );
343 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
346 AIS_ListOfInteractive List;
347 myAISContext->DisplayedObjects(List);
351 AIS_ListOfInteractive List1;
352 myAISContext->ObjectsInCollector(List1);
356 AIS_ListIteratorOfListOfInteractive ite(List);
357 for ( ; ite.More(); ite.Next() )
358 if( ite.Value()==obj )
364 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
366 return myAISContext->IsDisplayed( obj );
369 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
375 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
376 color.green() / 255.,
379 obj->SetColor( CSFColor );
383 myV3dViewer->Update();
386 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
387 int mode, bool update )
389 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
391 myV3dViewer->Update();
394 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
395 float trans, bool update )
397 myAISContext->SetTransparency( obj, trans, false );
398 myAISContext->Redisplay( obj, Standard_False, Standard_True );
400 myV3dViewer->Update();
403 //****************************************************************
404 void OCCViewer_Viewer::toggleTrihedron()
406 setTrihedronShown( !isTrihedronVisible() );
409 bool OCCViewer_Viewer::isTrihedronVisible() const
411 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
414 void OCCViewer_Viewer::setTrihedronShown( const bool on )
416 if ( myTrihedron.IsNull() )
420 myAISContext->Display( myTrihedron );
422 myAISContext->Erase( myTrihedron );
425 int OCCViewer_Viewer::trihedronSize() const
428 if ( !myTrihedron.IsNull() )
429 sz = (int)myTrihedron->Size();
433 void OCCViewer_Viewer::setTrihedronSize( const int sz )
435 if ( !myTrihedron.IsNull() )
436 myTrihedron->SetSize( sz );
439 void OCCViewer_Viewer::setIsos( const int u, const int v )
441 Handle(AIS_InteractiveContext) ic = getAISContext();
445 ic->SetIsoNumber( u, AIS_TOI_IsoU );
446 ic->SetIsoNumber( v, AIS_TOI_IsoV );
449 void OCCViewer_Viewer::isos( int& u, int& v ) const
451 Handle(AIS_InteractiveContext) ic = getAISContext();
454 u = ic->IsoNumber( AIS_TOI_IsoU );
455 v = ic->IsoNumber( AIS_TOI_IsoV );