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);
44 /* create trihedron */
45 if( DisplayTrihedron )
47 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
48 myTrihedron = new AIS_Trihedron(anAxis);
49 myTrihedron->SetInfiniteState( Standard_True );
51 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
52 //myTrihedron->SetColor( Col );
53 myTrihedron->SetArrowColor( Col.Name() );
54 myTrihedron->SetSize(100);
55 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
56 if (drawer->HasDatumAspect()) {
57 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
58 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
59 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
60 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
63 myAISContext->Display(myTrihedron);
64 myAISContext->Deactivate(myTrihedron);
68 mySelectionEnabled = true;
69 myMultiSelectionEnabled = true;
73 OCCViewer_Viewer::~OCCViewer_Viewer()
77 QColor OCCViewer_Viewer::backgroundColor() const
82 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
88 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
90 OCCViewer_ViewWindow* res = new OCCViewer_ViewWindow(theDesktop, this);
93 OCCViewer_ViewPort3d* vp3d = res->getViewPort();
95 vp3d->setBackgroundColor( myBgColor );
100 //*********************************************************************
101 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
103 SUIT_ViewModel::setViewManager(theViewManager);
104 if (theViewManager) {
105 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
106 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
108 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
109 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
111 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
112 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
117 //*********************************************************************
118 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
120 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
124 //*********************************************************************
125 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
127 if (!mySelectionEnabled) return;
128 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
130 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
131 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
135 //*********************************************************************
136 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
138 if (!mySelectionEnabled) return;
139 if (theEvent->button() != Qt::LeftButton) return;
140 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
143 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
144 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
145 bool aHasShift = (theEvent->state() & Qt::ShiftButton);
147 if (myStartPnt == myEndPnt)
149 if (aHasShift && myMultiSelectionEnabled)
150 myAISContext->ShiftSelect();
152 myAISContext->Select();
156 if (aHasShift && myMultiSelectionEnabled)
157 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
158 myEndPnt.x(), myEndPnt.y(),
159 aView->getViewPort()->getView(), Standard_False );
161 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
162 myEndPnt.x(), myEndPnt.y(),
163 aView->getViewPort()->getView(), Standard_False );
165 int Nb = myAISContext->NbSelected();
166 if( Nb>1 && !myMultiSelectionEnabled )
168 myAISContext->InitSelected();
169 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
170 if( !anOwner.IsNull() )
172 myAISContext->ClearSelected( Standard_False );
173 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
177 myAISContext->UpdateCurrentViewer();
179 emit selectionChanged();
183 //*********************************************************************
184 void OCCViewer_Viewer::enableSelection(bool isEnabled)
186 mySelectionEnabled = isEnabled;
187 //!! To be done for view windows
190 //*********************************************************************
191 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
193 myMultiSelectionEnabled = isEnable;
194 //!! To be done for view windows
197 //*********************************************************************
198 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
200 thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
201 thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
203 thePopup->insertSeparator();
205 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
206 if ( aView && !aView->getToolBar()->isVisible() )
207 thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
210 void OCCViewer_Viewer::onDumpView()
212 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
217 //*********************************************************************
218 void OCCViewer_Viewer::onChangeBgColor()
220 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
223 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
226 QColor aColorActive = aViewPort3d->backgroundColor();
228 QColor selColor = QColorDialog::getColor( aColorActive, aView);
229 if ( selColor.isValid() )
230 aViewPort3d->setBackgroundColor(selColor);
233 //*********************************************************************
234 void OCCViewer_Viewer::onShowToolbar() {
235 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
237 aView->getToolBar()->show();
240 //*********************************************************************
241 void OCCViewer_Viewer::update()
243 if (!myV3dViewer.IsNull())
244 myV3dViewer->Update();
247 //*********************************************************************
248 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
251 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
252 theList.Append(myAISContext->SelectedInteractive());
255 //*********************************************************************
256 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
258 AIS_ListIteratorOfListOfInteractive aIt;
259 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
260 myAISContext->SetSelected(aIt.Value(), false);
261 myAISContext->UpdateCurrentViewer();
264 //*********************************************************************
265 void OCCViewer_Viewer::performSelectionChanged()
267 emit selectionChanged();
270 //****************************************************************
272 void OCCViewer_Viewer::onClearViewAspects()
277 //****************************************************************
279 void OCCViewer_Viewer::clearViewAspects()
281 myViewAspects.clear();
284 //****************************************************************
286 const viewAspectList& OCCViewer_Viewer::getViewAspects()
288 return myViewAspects;
291 //****************************************************************
293 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
295 myViewAspects.append( aParams );
298 //****************************************************************
300 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
302 myViewAspects = aViewList;
305 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
306 bool hilight, bool update )
308 bool isInLocal = myAISContext->HasOpenedContext();
312 if ( hilight && !myAISContext->IsSelected( obj ) )
313 myAISContext->AddOrRemoveCurrentObject( obj, false );
314 else if ( !hilight && myAISContext->IsSelected( obj ) )
315 myAISContext->AddOrRemoveCurrentObject( obj, false );
319 myV3dViewer->Redraw();
324 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
326 if ( myAISContext->HasOpenedContext() )
327 myAISContext->ClearSelected( updateviewer );
329 myAISContext->ClearCurrents( updateviewer );
333 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
336 AIS_ListOfInteractive List;
337 myAISContext->DisplayedObjects(List);
341 AIS_ListOfInteractive List1;
342 myAISContext->ObjectsInCollector(List1);
346 AIS_ListIteratorOfListOfInteractive ite(List);
347 for ( ; ite.More(); ite.Next() )
348 if( ite.Value()==obj )
354 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
356 return myAISContext->IsDisplayed( obj );
359 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
365 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
366 color.green() / 255.,
369 obj->SetColor( CSFColor );
373 myV3dViewer->Update();
376 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
377 int mode, bool update )
379 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
381 myV3dViewer->Update();
384 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
385 float trans, bool update )
387 myAISContext->SetTransparency( obj, trans, false );
388 myAISContext->Redisplay( obj, Standard_False, Standard_True );
390 myV3dViewer->Update();
393 //****************************************************************
394 void OCCViewer_Viewer::toggleTrihedron()
396 setTrihedronShown( !isTrihedronVisible() );
399 bool OCCViewer_Viewer::isTrihedronVisible() const
401 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
404 void OCCViewer_Viewer::setTrihedronShown( const bool on )
406 if ( myTrihedron.IsNull() )
410 myAISContext->Display( myTrihedron );
412 myAISContext->Erase( myTrihedron );
415 int OCCViewer_Viewer::trihedronSize() const
418 if ( !myTrihedron.IsNull() )
419 sz = myTrihedron->Size();
423 void OCCViewer_Viewer::setTrihedronSize( const int sz )
425 if ( !myTrihedron.IsNull() )
426 myTrihedron->SetSize( sz );