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 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
93 OCCViewer_ViewWindow* res = new OCCViewer_ViewWindow(theDesktop, this);
96 OCCViewer_ViewPort3d* vp3d = res->getViewPort();
98 vp3d->setBackgroundColor( myBgColor );
103 //*********************************************************************
104 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
106 SUIT_ViewModel::setViewManager(theViewManager);
107 if (theViewManager) {
108 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
109 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
111 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
112 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
114 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
115 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
120 //*********************************************************************
121 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
123 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
127 //*********************************************************************
128 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
130 if (!mySelectionEnabled) return;
131 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
133 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
134 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
138 //*********************************************************************
139 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
141 if (!mySelectionEnabled) return;
142 if (theEvent->button() != Qt::LeftButton) return;
143 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
146 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
147 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
148 bool aHasShift = (theEvent->state() & Qt::ShiftButton);
150 if (myStartPnt == myEndPnt)
152 if (aHasShift && myMultiSelectionEnabled)
153 myAISContext->ShiftSelect();
155 myAISContext->Select();
159 if (aHasShift && myMultiSelectionEnabled)
160 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
161 myEndPnt.x(), myEndPnt.y(),
162 aView->getViewPort()->getView(), Standard_False );
164 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
165 myEndPnt.x(), myEndPnt.y(),
166 aView->getViewPort()->getView(), Standard_False );
168 int Nb = myAISContext->NbSelected();
169 if( Nb>1 && !myMultiSelectionEnabled )
171 myAISContext->InitSelected();
172 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
173 if( !anOwner.IsNull() )
175 myAISContext->ClearSelected( Standard_False );
176 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
180 myAISContext->UpdateCurrentViewer();
182 emit selectionChanged();
186 //*********************************************************************
187 void OCCViewer_Viewer::enableSelection(bool isEnabled)
189 mySelectionEnabled = isEnabled;
190 //!! To be done for view windows
193 //*********************************************************************
194 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
196 myMultiSelectionEnabled = isEnable;
197 //!! To be done for view windows
200 //*********************************************************************
201 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
203 thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
204 thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
206 thePopup->insertSeparator();
208 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
209 if ( aView && !aView->getToolBar()->isVisible() )
210 thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
213 void OCCViewer_Viewer::onDumpView()
215 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
220 //*********************************************************************
221 void OCCViewer_Viewer::onChangeBgColor()
223 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
226 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
229 QColor aColorActive = aViewPort3d->backgroundColor();
231 QColor selColor = QColorDialog::getColor( aColorActive, aView);
232 if ( selColor.isValid() )
233 aViewPort3d->setBackgroundColor(selColor);
236 //*********************************************************************
237 void OCCViewer_Viewer::onShowToolbar() {
238 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
240 aView->getToolBar()->show();
243 //*********************************************************************
244 void OCCViewer_Viewer::update()
246 if (!myV3dViewer.IsNull())
247 myV3dViewer->Update();
250 //*********************************************************************
251 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
254 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
255 theList.Append(myAISContext->SelectedInteractive());
258 //*********************************************************************
259 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
261 AIS_ListIteratorOfListOfInteractive aIt;
262 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
263 myAISContext->SetSelected(aIt.Value(), false);
264 myAISContext->UpdateCurrentViewer();
267 //*********************************************************************
268 void OCCViewer_Viewer::performSelectionChanged()
270 emit selectionChanged();
273 //****************************************************************
275 void OCCViewer_Viewer::onClearViewAspects()
280 //****************************************************************
282 void OCCViewer_Viewer::clearViewAspects()
284 myViewAspects.clear();
287 //****************************************************************
289 const viewAspectList& OCCViewer_Viewer::getViewAspects()
291 return myViewAspects;
294 //****************************************************************
296 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
298 myViewAspects.append( aParams );
301 //****************************************************************
303 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
305 myViewAspects = aViewList;
308 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
309 bool hilight, bool update )
311 bool isInLocal = myAISContext->HasOpenedContext();
315 if ( hilight && !myAISContext->IsSelected( obj ) )
316 myAISContext->AddOrRemoveCurrentObject( obj, false );
317 else if ( !hilight && myAISContext->IsSelected( obj ) )
318 myAISContext->AddOrRemoveCurrentObject( obj, false );
322 myV3dViewer->Redraw();
327 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
329 if ( myAISContext->HasOpenedContext() )
330 myAISContext->ClearSelected( updateviewer );
332 myAISContext->ClearCurrents( updateviewer );
336 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
339 AIS_ListOfInteractive List;
340 myAISContext->DisplayedObjects(List);
344 AIS_ListOfInteractive List1;
345 myAISContext->ObjectsInCollector(List1);
349 AIS_ListIteratorOfListOfInteractive ite(List);
350 for ( ; ite.More(); ite.Next() )
351 if( ite.Value()==obj )
357 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
359 return myAISContext->IsDisplayed( obj );
362 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
368 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
369 color.green() / 255.,
372 obj->SetColor( CSFColor );
376 myV3dViewer->Update();
379 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
380 int mode, bool update )
382 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
384 myV3dViewer->Update();
387 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
388 float trans, bool update )
390 myAISContext->SetTransparency( obj, trans, false );
391 myAISContext->Redisplay( obj, Standard_False, Standard_True );
393 myV3dViewer->Update();
396 //****************************************************************
397 void OCCViewer_Viewer::toggleTrihedron()
399 setTrihedronShown( !isTrihedronVisible() );
402 bool OCCViewer_Viewer::isTrihedronVisible() const
404 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
407 void OCCViewer_Viewer::setTrihedronShown( const bool on )
409 if ( myTrihedron.IsNull() )
413 myAISContext->Display( myTrihedron );
415 myAISContext->Erase( myTrihedron );
418 int OCCViewer_Viewer::trihedronSize() const
421 if ( !myTrihedron.IsNull() )
422 sz = (int)myTrihedron->Size();
426 void OCCViewer_Viewer::setTrihedronSize( const int sz )
428 if ( !myTrihedron.IsNull() )
429 myTrihedron->SetSize( sz );
432 void OCCViewer_Viewer::setIsos( const int u, const int v )
434 Handle(AIS_InteractiveContext) ic = getAISContext();
438 ic->SetIsoNumber( u, AIS_TOI_IsoU );
439 ic->SetIsoNumber( u, AIS_TOI_IsoV );
442 void OCCViewer_Viewer::isos( int& u, int& v ) const
444 Handle(AIS_InteractiveContext) ic = getAISContext();
447 u = ic->IsoNumber( AIS_TOI_IsoU );
448 v = ic->IsoNumber( AIS_TOI_IsoV );