1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "OCCViewer_ViewModel.h"
23 #include "OCCViewer_ViewWindow.h"
24 #include "OCCViewer_VService.h"
25 #include "OCCViewer_ViewPort3d.h"
27 #include "SUIT_ViewWindow.h"
28 #include "SUIT_ViewManager.h"
29 #include "SUIT_Desktop.h"
30 #include "SUIT_Session.h"
32 #include "QtxActionToolMgr.h"
35 #include <QApplication>
36 #include <QColorDialog>
39 #include <QMouseEvent>
41 #include <QDesktopWidget>
43 #include <AIS_Axis.hxx>
44 #include <AIS_Drawer.hxx>
45 #include <AIS_ListOfInteractive.hxx>
46 #include <AIS_ListIteratorOfListOfInteractive.hxx>
48 #include <Geom_Axis2Placement.hxx>
49 #include <Prs3d_Drawer.hxx>
50 #include <Prs3d_DatumAspect.hxx>
51 #include <Prs3d_LineAspect.hxx>
52 #include <Prs3d_LengthAspect.hxx>
53 #include <Prs3d_AngleAspect.hxx>
54 #include <Prs3d_TextAspect.hxx>
58 \param DisplayTrihedron - is trihedron displayed
60 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron, bool DisplayStaticTrihedron )
62 myBgColor( Qt::black ),
63 myShowStaticTrihedron( DisplayStaticTrihedron )
65 // init CasCade viewers
66 myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
67 V3d_XposYnegZpos, true, true );
71 myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
72 V3d_XposYnegZpos, true, true );
73 myV3dCollector->Init();
76 myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
78 myAISContext->SelectionColor( Quantity_NOC_WHITE );
80 // display isoline on planar faces (box for ex.)
81 myAISContext->IsoOnPlane( true );
83 double h = QApplication::desktop()->screenGeometry( QApplication::desktop()->primaryScreen() ).height() / 300. ;
84 Handle(Prs3d_Drawer) drawer = myAISContext->DefaultDrawer();
85 Handle(Prs3d_TextAspect) ta = drawer->TextAspect();
86 ta->SetHeight(100); // VSR: workaround for CAS.CADE bug (is it really needed ???)
88 drawer->SetTextAspect(ta);
89 drawer->AngleAspect()->SetTextAspect(ta);
90 drawer->LengthAspect()->SetTextAspect(ta);
94 /* create trihedron */
95 if( DisplayTrihedron )
97 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
98 myTrihedron = new AIS_Trihedron(anAxis);
99 myTrihedron->SetInfiniteState( Standard_True );
101 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
102 //myTrihedron->SetColor( Col );
103 myTrihedron->SetArrowColor( Col.Name() );
104 myTrihedron->SetSize(100);
105 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
106 if (drawer->HasDatumAspect()) {
107 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
108 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
109 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
110 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
113 myAISContext->Display(myTrihedron);
114 myAISContext->Deactivate(myTrihedron);
118 mySelectionEnabled = true;
119 myMultiSelectionEnabled = true;
125 OCCViewer_Viewer::~OCCViewer_Viewer()
130 \return background color of viewer
132 QColor OCCViewer_Viewer::backgroundColor() const
138 Sets background color
139 \param c - new background color
141 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
148 Start initialization of view window
149 \param view - view window to be initialized
151 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
155 view->initSketchers();
157 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
159 vp3d->setBackgroundColor( myBgColor );
164 Creates new view window
165 \param theDesktop - main window of application
167 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
169 OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
175 Sets new view manager
176 \param theViewManager - new view manager
178 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
180 SUIT_ViewModel::setViewManager(theViewManager);
181 if (theViewManager) {
182 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
183 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
185 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
186 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
188 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
189 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
194 SLOT: called on mouse button press, stores current mouse position as start point for transformations
196 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
198 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
202 SLOT: called on mouse move, processes transformation or hilighting
204 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
206 if (!mySelectionEnabled) return;
207 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
209 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
211 if ( isSelectionEnabled() ) {
212 if (aView->getViewPort()->isBusy()) return; // Check that the ViewPort initialization completed
213 // To Prevent call move event if the View port is not initialized
215 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
216 if ( !aView3d.IsNull() )
217 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
223 SLOT: called on mouse button release, finishes transformation or selection
225 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
227 if (!mySelectionEnabled) return;
228 if (theEvent->button() != Qt::LeftButton) return;
229 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
232 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
233 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
234 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
236 if (!aHasShift) emit deselection();
238 if (myStartPnt == myEndPnt)
240 if (aHasShift && myMultiSelectionEnabled)
241 myAISContext->ShiftSelect();
243 myAISContext->Select();
247 if (aHasShift && myMultiSelectionEnabled)
248 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
249 myEndPnt.x(), myEndPnt.y(),
250 aView->getViewPort()->getView(), Standard_False );
252 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
253 myEndPnt.x(), myEndPnt.y(),
254 aView->getViewPort()->getView(), Standard_False );
256 int Nb = myAISContext->NbSelected();
257 if( Nb>1 && !myMultiSelectionEnabled )
259 myAISContext->InitSelected();
260 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
261 if( !anOwner.IsNull() )
263 myAISContext->ClearSelected( Standard_False );
264 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
268 myAISContext->UpdateCurrentViewer();
270 emit selectionChanged();
275 Sets selection enabled status
276 \param isEnabled - new status
278 void OCCViewer_Viewer::enableSelection(bool isEnabled)
280 mySelectionEnabled = isEnabled;
281 //!! To be done for view windows
282 if ( !myViewManager )
285 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
286 for ( int i = 0; i < (int)wins.count(); i++ )
288 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
290 win->updateEnabledDrawMode();
295 Sets multiselection enabled status
296 \param isEnabled - new status
298 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
300 myMultiSelectionEnabled = isEnable;
301 //!! To be done for view windows
302 if ( !myViewManager )
305 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
306 for ( int i = 0; i < (int)wins.count(); i++ )
308 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
310 win->updateEnabledDrawMode();
315 Builds popup for occ viewer
317 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
319 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
320 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
322 thePopup->addSeparator();
324 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
326 //Support of several toolbars in the popup menu
327 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
328 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
329 for( ; it!=last; it++ )
330 thePopup->addAction( (*it)->toggleViewAction() );
334 SLOT: called on dump view operation is activated, stores scene to raster file
336 void OCCViewer_Viewer::onDumpView()
338 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
344 SLOT: called if background color is to be changed changed, passes new color to view port
346 void OCCViewer_Viewer::onChangeBgColor()
348 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
351 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
354 QColor aColorActive = aViewPort3d->backgroundColor();
356 QColor selColor = QColorDialog::getColor( aColorActive, aView);
357 if ( selColor.isValid() )
358 aViewPort3d->setBackgroundColor(selColor);
362 Updates OCC 3D viewer
364 void OCCViewer_Viewer::update()
366 if (!myV3dViewer.IsNull())
367 myV3dViewer->Update();
369 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
371 aView->updateGravityCoords();
375 \return objects selected in 3D viewer
376 \param theList - list to be filled with selected objects
378 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
381 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
382 theList.Append(myAISContext->SelectedInteractive());
386 Selects objects in 3D viewer. Other selected objects are left as selected
387 \param theList - list objects to be selected
389 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
391 AIS_ListIteratorOfListOfInteractive aIt;
392 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
393 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
394 myAISContext->UpdateCurrentViewer();
398 Auxiliary method to emit signal selectionChanged()
400 void OCCViewer_Viewer::performSelectionChanged()
402 emit selectionChanged();
406 SLOT, clears view aspects
408 void OCCViewer_Viewer::onClearViewAspects()
416 void OCCViewer_Viewer::clearViewAspects()
418 myViewAspects.clear();
422 \return const reference to list of view aspects
424 const viewAspectList& OCCViewer_Viewer::getViewAspects()
426 return myViewAspects;
430 Appends new view aspect
431 \param aParams - new view aspects
433 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
435 myViewAspects.append( aParams );
439 Replaces old view aspects by new ones
440 \param aViewList - list of new view aspects
442 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
444 myViewAspects = aViewList;
448 Hilights/unhilights object in viewer
449 \param obj - object to be updated
450 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
451 \param update - update current viewer
453 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
454 bool hilight, bool update )
456 bool isInLocal = myAISContext->HasOpenedContext();
460 if ( hilight && !myAISContext->IsSelected( obj ) )
461 myAISContext->AddOrRemoveCurrentObject( obj, false );
462 else if ( !hilight && myAISContext->IsSelected( obj ) )
463 myAISContext->AddOrRemoveCurrentObject( obj, false );
467 myV3dViewer->Redraw();
473 Unhilights all objects in viewer
474 \param updateviewer - update current viewer
476 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
478 if ( myAISContext->HasOpenedContext() )
479 myAISContext->ClearSelected( updateviewer );
481 myAISContext->ClearCurrents( updateviewer );
486 \return true if object is in viewer or in collector
487 \param obj - object to be checked
488 \param onlyInViewer - search object only in viewer (so object must be displayed)
490 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
493 AIS_ListOfInteractive List;
494 myAISContext->DisplayedObjects(List);
498 AIS_ListOfInteractive List1;
499 myAISContext->ObjectsInCollector(List1);
503 AIS_ListIteratorOfListOfInteractive ite(List);
504 for ( ; ite.More(); ite.Next() )
505 if( ite.Value()==obj )
512 \return true if object is displayed in viewer
513 \param obj - object to be checked
515 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
517 return myAISContext->IsDisplayed( obj );
522 \param obj - object to be updated
523 \param color - new color
524 \param update - update current viewer
526 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
532 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
533 color.green() / 255.,
536 obj->SetColor( CSFColor );
540 myV3dViewer->Update();
544 Changes display mode of object
545 \param obj - object to be processed
546 \param mode - new display mode
547 \param update - update current viewer
549 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
550 int mode, bool update )
552 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
554 myV3dViewer->Update();
558 Changes transparency of object
559 \param obj - object to be processed
560 \param trans - new transparency
561 \param update - update current viewer
563 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
564 float trans, bool update )
566 myAISContext->SetTransparency( obj, trans, false );
567 myAISContext->Redisplay( obj, Standard_False, Standard_True );
569 myV3dViewer->Update();
573 Changes visibility of trihedron to opposite
575 void OCCViewer_Viewer::toggleTrihedron()
577 setTrihedronShown( !isTrihedronVisible() );
581 \return true if trihedron is visible
583 bool OCCViewer_Viewer::isTrihedronVisible() const
585 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
589 Sets visibility state of trihedron
590 \param on - new state
593 void OCCViewer_Viewer::setTrihedronShown( const bool on )
595 if ( myTrihedron.IsNull() )
599 myAISContext->Display( myTrihedron );
601 myAISContext->Erase( myTrihedron );
605 \return trihedron size
607 double OCCViewer_Viewer::trihedronSize() const
610 if ( !myTrihedron.IsNull() )
611 sz = myTrihedron->Size();
616 Changes trihedron size
619 void OCCViewer_Viewer::setTrihedronSize( const double sz )
621 if ( !myTrihedron.IsNull() )
622 myTrihedron->SetSize( sz );
626 Set number of isolines
627 \param u - u-isolines (first parametric co-ordinate)
628 \param v - v-isolines (second parametric co-ordinate)
630 void OCCViewer_Viewer::setIsos( const int u, const int v )
632 Handle(AIS_InteractiveContext) ic = getAISContext();
636 ic->SetIsoNumber( u, AIS_TOI_IsoU );
637 ic->SetIsoNumber( v, AIS_TOI_IsoV );
641 \return number of isolines
642 \param u - to return u-isolines (first parametric co-ordinate)
643 \param v - to return v-isolines (second parametric co-ordinate)
645 void OCCViewer_Viewer::isos( int& u, int& v ) const
647 Handle(AIS_InteractiveContext) ic = getAISContext();
650 u = ic->IsoNumber( AIS_TOI_IsoU );
651 v = ic->IsoNumber( AIS_TOI_IsoV );