1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "OCCViewer_ViewModel.h"
21 #include "OCCViewer_ViewWindow.h"
22 #include "OCCViewer_VService.h"
23 #include "OCCViewer_ViewPort3d.h"
25 #include "SUIT_ViewWindow.h"
26 #include "SUIT_Desktop.h"
27 #include "SUIT_Session.h"
30 #include <qapplication.h>
31 #include <qcolordialog.h>
33 #include <qpopupmenu.h>
35 #include <AIS_Axis.hxx>
36 #include <AIS_Drawer.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
39 #include <Geom_Axis2Placement.hxx>
40 #include <Prs3d_DatumAspect.hxx>
41 #include <Prs3d_LineAspect.hxx>
45 \param DisplayTrihedron - is trihedron displayed
47 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron )
49 myBgColor( Qt::black )
51 // init CasCade viewers
52 myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
53 V3d_XposYnegZpos, true, true );
57 myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
58 V3d_XposYnegZpos, true, true );
59 myV3dCollector->Init();
62 myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
64 myAISContext->SelectionColor( Quantity_NOC_WHITE );
66 // display isoline on planar faces (box for ex.)
67 myAISContext->IsoOnPlane( true );
71 /* create trihedron */
72 if( DisplayTrihedron )
74 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
75 myTrihedron = new AIS_Trihedron(anAxis);
76 myTrihedron->SetInfiniteState( Standard_True );
78 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
79 //myTrihedron->SetColor( Col );
80 myTrihedron->SetArrowColor( Col.Name() );
81 myTrihedron->SetSize(100);
82 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
83 if (drawer->HasDatumAspect()) {
84 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
85 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
86 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
87 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
90 myAISContext->Display(myTrihedron);
91 myAISContext->Deactivate(myTrihedron);
95 mySelectionEnabled = true;
96 myMultiSelectionEnabled = true;
102 OCCViewer_Viewer::~OCCViewer_Viewer()
107 \return background color of viewer
109 QColor OCCViewer_Viewer::backgroundColor() const
115 Sets background color
116 \param c - new background color
118 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
125 Start initialization of view window
126 \param view - view window to be initialized
128 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
133 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
135 vp3d->setBackgroundColor( myBgColor );
140 Creates new view window
141 \param theDesktop - main window of application
143 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
145 OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
151 Sets new view manager
152 \param theViewManager - new view manager
154 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
156 SUIT_ViewModel::setViewManager(theViewManager);
157 if (theViewManager) {
158 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
159 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
161 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
162 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
164 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
165 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
170 SLOT: called on mouse button press, stores current mouse position as start point for transformations
172 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
174 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
178 SLOT: called on mouse move, processes transformation or hilighting
180 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
182 if (!mySelectionEnabled) return;
183 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
185 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
186 if ( isSelectionEnabled() )
187 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
192 SLOT: called on mouse button release, finishes transformation or selection
194 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
196 if (!mySelectionEnabled) return;
197 if (theEvent->button() != Qt::LeftButton) return;
198 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
201 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
202 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
203 bool aHasShift = (theEvent->state() & Qt::ShiftButton);
205 if (myStartPnt == myEndPnt)
207 if (aHasShift && myMultiSelectionEnabled)
208 myAISContext->ShiftSelect();
210 myAISContext->Select();
214 if (aHasShift && myMultiSelectionEnabled)
215 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
216 myEndPnt.x(), myEndPnt.y(),
217 aView->getViewPort()->getView(), Standard_False );
219 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
220 myEndPnt.x(), myEndPnt.y(),
221 aView->getViewPort()->getView(), Standard_False );
223 int Nb = myAISContext->NbSelected();
224 if( Nb>1 && !myMultiSelectionEnabled )
226 myAISContext->InitSelected();
227 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
228 if( !anOwner.IsNull() )
230 myAISContext->ClearSelected( Standard_False );
231 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
235 myAISContext->UpdateCurrentViewer();
237 emit selectionChanged();
242 Sets selection enabled status
243 \param isEnabled - new status
245 void OCCViewer_Viewer::enableSelection(bool isEnabled)
247 mySelectionEnabled = isEnabled;
248 //!! To be done for view windows
249 if ( !myViewManager )
252 QPtrVector<SUIT_ViewWindow> wins = myViewManager->getViews();
253 for ( int i = 0; i < wins.count(); i++ )
255 OCCViewer_ViewWindow* win = ::qt_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
257 win->updateEnabledDrawMode();
262 Sets multiselection enabled status
263 \param isEnabled - new status
265 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
267 myMultiSelectionEnabled = isEnable;
268 //!! To be done for view windows
269 if ( !myViewManager )
272 QPtrVector<SUIT_ViewWindow> wins = myViewManager->getViews();
273 for ( int i = 0; i < wins.count(); i++ )
275 OCCViewer_ViewWindow* win = ::qt_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
277 win->updateEnabledDrawMode();
282 Builds popup for occ viewer
284 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
286 thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
287 thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
289 thePopup->insertSeparator();
291 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
292 if ( aView && !aView->getToolBar()->isVisible() )
293 thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
297 SLOT: called on dump view operation is activated, stores scene to raster file
299 void OCCViewer_Viewer::onDumpView()
301 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
307 SLOT: called if background color is to be changed changed, passes new color to view port
309 void OCCViewer_Viewer::onChangeBgColor()
311 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
314 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
317 QColor aColorActive = aViewPort3d->backgroundColor();
319 QColor selColor = QColorDialog::getColor( aColorActive, aView);
320 if ( selColor.isValid() )
321 aViewPort3d->setBackgroundColor(selColor);
325 SLOT: called when popup item "Show toolbar" is activated, shows toolbar of active view window
327 void OCCViewer_Viewer::onShowToolbar() {
328 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
330 aView->getToolBar()->show();
334 Updates OCC 3D viewer
336 void OCCViewer_Viewer::update()
338 if (!myV3dViewer.IsNull())
339 myV3dViewer->Update();
343 \return objects selected in 3D viewer
344 \param theList - list to be filled with selected objects
346 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
349 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
350 theList.Append(myAISContext->SelectedInteractive());
354 Selects objects in 3D viewer. Other selected objects are left as selected
355 \param theList - list objects to be selected
357 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
359 AIS_ListIteratorOfListOfInteractive aIt;
360 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
361 myAISContext->SetSelected(aIt.Value(), false);
362 myAISContext->UpdateCurrentViewer();
366 Auxiliary method to emit signal selectionChanged()
368 void OCCViewer_Viewer::performSelectionChanged()
370 emit selectionChanged();
374 SLOT, clears view aspects
376 void OCCViewer_Viewer::onClearViewAspects()
384 void OCCViewer_Viewer::clearViewAspects()
386 myViewAspects.clear();
390 \return const reference to list of view aspects
392 const viewAspectList& OCCViewer_Viewer::getViewAspects()
394 return myViewAspects;
398 Appends new view aspect
399 \param aParams - new view aspects
401 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
403 myViewAspects.append( aParams );
407 Replaces old view aspects by new ones
408 \param aViewList - list of new view aspects
410 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
412 myViewAspects = aViewList;
416 Hilights/unhilights object in viewer
417 \param obj - object to be updated
418 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
419 \param update - update current viewer
421 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
422 bool hilight, bool update )
424 bool isInLocal = myAISContext->HasOpenedContext();
428 if ( hilight && !myAISContext->IsSelected( obj ) )
429 myAISContext->AddOrRemoveCurrentObject( obj, false );
430 else if ( !hilight && myAISContext->IsSelected( obj ) )
431 myAISContext->AddOrRemoveCurrentObject( obj, false );
435 myV3dViewer->Redraw();
441 Unhilights all objects in viewer
442 \param updateviewer - update current viewer
444 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
446 if ( myAISContext->HasOpenedContext() )
447 myAISContext->ClearSelected( updateviewer );
449 myAISContext->ClearCurrents( updateviewer );
454 \return true if object is in viewer or in collector
455 \param obj - object to be checked
456 \param onlyInViewer - search object only in viewer (so object must be displayed)
458 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
461 AIS_ListOfInteractive List;
462 myAISContext->DisplayedObjects(List);
466 AIS_ListOfInteractive List1;
467 myAISContext->ObjectsInCollector(List1);
471 AIS_ListIteratorOfListOfInteractive ite(List);
472 for ( ; ite.More(); ite.Next() )
473 if( ite.Value()==obj )
480 \return true if object is displayed in viewer
481 \param obj - object to be checked
483 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
485 return myAISContext->IsDisplayed( obj );
490 \param obj - object to be updated
491 \param color - new color
492 \param update - update current viewer
494 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
500 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
501 color.green() / 255.,
504 obj->SetColor( CSFColor );
508 myV3dViewer->Update();
512 Changes display mode of object
513 \param obj - object to be processed
514 \param mode - new display mode
515 \param update - update current viewer
517 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
518 int mode, bool update )
520 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
522 myV3dViewer->Update();
526 Changes transparency of object
527 \param obj - object to be processed
528 \param trans - new transparency
529 \param update - update current viewer
531 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
532 float trans, bool update )
534 myAISContext->SetTransparency( obj, trans, false );
535 myAISContext->Redisplay( obj, Standard_False, Standard_True );
537 myV3dViewer->Update();
541 Changes visibility of trihedron to opposite
543 void OCCViewer_Viewer::toggleTrihedron()
545 setTrihedronShown( !isTrihedronVisible() );
549 \return true if trihedron is visible
551 bool OCCViewer_Viewer::isTrihedronVisible() const
553 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
557 Sets visibility state of trihedron
558 \param on - new state
561 void OCCViewer_Viewer::setTrihedronShown( const bool on )
563 if ( myTrihedron.IsNull() )
567 myAISContext->Display( myTrihedron );
569 myAISContext->Erase( myTrihedron );
573 \return trihedron size
575 int OCCViewer_Viewer::trihedronSize() const
578 if ( !myTrihedron.IsNull() )
579 sz = (int)myTrihedron->Size();
584 Changes trihedron size
587 void OCCViewer_Viewer::setTrihedronSize( const int sz )
589 if ( !myTrihedron.IsNull() )
590 myTrihedron->SetSize( sz );
594 Set number of isolines
595 \param u - u-isolines (first parametric co-ordinate)
596 \param v - v-isolines (second parametric co-ordinate)
598 void OCCViewer_Viewer::setIsos( const int u, const int v )
600 Handle(AIS_InteractiveContext) ic = getAISContext();
604 ic->SetIsoNumber( u, AIS_TOI_IsoU );
605 ic->SetIsoNumber( v, AIS_TOI_IsoV );
609 \return number of isolines
610 \param u - to return u-isolines (first parametric co-ordinate)
611 \param v - to return v-isolines (second parametric co-ordinate)
613 void OCCViewer_Viewer::isos( int& u, int& v ) const
615 Handle(AIS_InteractiveContext) ic = getAISContext();
618 u = ic->IsoNumber( AIS_TOI_IsoU );
619 v = ic->IsoNumber( AIS_TOI_IsoV );