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/
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>
43 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron )
45 myBgColor( Qt::black )
47 // init CasCade viewers
48 myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
49 V3d_XposYnegZpos, true, true );
53 myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
54 V3d_XposYnegZpos, true, true );
55 myV3dCollector->Init();
58 myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
60 myAISContext->SelectionColor( Quantity_NOC_WHITE );
62 // display isoline on planar faces (box for ex.)
63 myAISContext->IsoOnPlane( true );
67 /* create trihedron */
68 if( DisplayTrihedron )
70 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
71 myTrihedron = new AIS_Trihedron(anAxis);
72 myTrihedron->SetInfiniteState( Standard_True );
74 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
75 //myTrihedron->SetColor( Col );
76 myTrihedron->SetArrowColor( Col.Name() );
77 myTrihedron->SetSize(100);
78 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
79 if (drawer->HasDatumAspect()) {
80 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
81 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
82 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
83 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
86 myAISContext->Display(myTrihedron);
87 myAISContext->Deactivate(myTrihedron);
91 mySelectionEnabled = true;
92 myMultiSelectionEnabled = true;
96 OCCViewer_Viewer::~OCCViewer_Viewer()
100 QColor OCCViewer_Viewer::backgroundColor() const
105 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
111 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
116 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
118 vp3d->setBackgroundColor( myBgColor );
123 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
125 OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
130 //*********************************************************************
131 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
133 SUIT_ViewModel::setViewManager(theViewManager);
134 if (theViewManager) {
135 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
136 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
138 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
139 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
141 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
142 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
147 //*********************************************************************
148 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
150 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
154 //*********************************************************************
155 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
157 if (!mySelectionEnabled) return;
158 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
160 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
161 if ( isSelectionEnabled() )
162 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
166 //*********************************************************************
167 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
169 if (!mySelectionEnabled) return;
170 if (theEvent->button() != Qt::LeftButton) return;
171 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
174 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
175 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
176 bool aHasShift = (theEvent->state() & Qt::ShiftButton);
178 if (myStartPnt == myEndPnt)
180 if (aHasShift && myMultiSelectionEnabled)
181 myAISContext->ShiftSelect();
183 myAISContext->Select();
187 if (aHasShift && myMultiSelectionEnabled)
188 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
189 myEndPnt.x(), myEndPnt.y(),
190 aView->getViewPort()->getView(), Standard_False );
192 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
193 myEndPnt.x(), myEndPnt.y(),
194 aView->getViewPort()->getView(), Standard_False );
196 int Nb = myAISContext->NbSelected();
197 if( Nb>1 && !myMultiSelectionEnabled )
199 myAISContext->InitSelected();
200 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
201 if( !anOwner.IsNull() )
203 myAISContext->ClearSelected( Standard_False );
204 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
208 myAISContext->UpdateCurrentViewer();
210 emit selectionChanged();
214 //*********************************************************************
215 void OCCViewer_Viewer::enableSelection(bool isEnabled)
217 mySelectionEnabled = isEnabled;
218 //!! To be done for view windows
219 if ( !myViewManager )
222 QPtrVector<SUIT_ViewWindow> wins = myViewManager->getViews();
223 for ( int i = 0; i < wins.count(); i++ )
225 OCCViewer_ViewWindow* win = ::qt_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
227 win->updateEnabledDrawMode();
231 //*********************************************************************
232 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
234 myMultiSelectionEnabled = isEnable;
235 //!! To be done for view windows
236 if ( !myViewManager )
239 QPtrVector<SUIT_ViewWindow> wins = myViewManager->getViews();
240 for ( int i = 0; i < wins.count(); i++ )
242 OCCViewer_ViewWindow* win = ::qt_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
244 win->updateEnabledDrawMode();
248 //*********************************************************************
249 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
251 thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
252 thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
254 thePopup->insertSeparator();
256 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
257 if ( aView && !aView->getToolBar()->isVisible() )
258 thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
261 void OCCViewer_Viewer::onDumpView()
263 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
268 //*********************************************************************
269 void OCCViewer_Viewer::onChangeBgColor()
271 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
274 OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
277 QColor aColorActive = aViewPort3d->backgroundColor();
279 QColor selColor = QColorDialog::getColor( aColorActive, aView);
280 if ( selColor.isValid() )
281 aViewPort3d->setBackgroundColor(selColor);
284 //*********************************************************************
285 void OCCViewer_Viewer::onShowToolbar() {
286 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
288 aView->getToolBar()->show();
291 //*********************************************************************
292 void OCCViewer_Viewer::update()
294 if (!myV3dViewer.IsNull())
295 myV3dViewer->Update();
298 //*********************************************************************
299 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
302 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
303 theList.Append(myAISContext->SelectedInteractive());
306 //*********************************************************************
307 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
309 AIS_ListIteratorOfListOfInteractive aIt;
310 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
311 myAISContext->SetSelected(aIt.Value(), false);
312 myAISContext->UpdateCurrentViewer();
315 //*********************************************************************
316 void OCCViewer_Viewer::performSelectionChanged()
318 emit selectionChanged();
321 //****************************************************************
323 void OCCViewer_Viewer::onClearViewAspects()
328 //****************************************************************
330 void OCCViewer_Viewer::clearViewAspects()
332 myViewAspects.clear();
335 //****************************************************************
337 const viewAspectList& OCCViewer_Viewer::getViewAspects()
339 return myViewAspects;
342 //****************************************************************
344 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
346 myViewAspects.append( aParams );
349 //****************************************************************
351 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
353 myViewAspects = aViewList;
356 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
357 bool hilight, bool update )
359 bool isInLocal = myAISContext->HasOpenedContext();
363 if ( hilight && !myAISContext->IsSelected( obj ) )
364 myAISContext->AddOrRemoveCurrentObject( obj, false );
365 else if ( !hilight && myAISContext->IsSelected( obj ) )
366 myAISContext->AddOrRemoveCurrentObject( obj, false );
370 myV3dViewer->Redraw();
375 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
377 if ( myAISContext->HasOpenedContext() )
378 myAISContext->ClearSelected( updateviewer );
380 myAISContext->ClearCurrents( updateviewer );
384 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
387 AIS_ListOfInteractive List;
388 myAISContext->DisplayedObjects(List);
392 AIS_ListOfInteractive List1;
393 myAISContext->ObjectsInCollector(List1);
397 AIS_ListIteratorOfListOfInteractive ite(List);
398 for ( ; ite.More(); ite.Next() )
399 if( ite.Value()==obj )
405 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
407 return myAISContext->IsDisplayed( obj );
410 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
416 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
417 color.green() / 255.,
420 obj->SetColor( CSFColor );
424 myV3dViewer->Update();
427 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
428 int mode, bool update )
430 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
432 myV3dViewer->Update();
435 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
436 float trans, bool update )
438 myAISContext->SetTransparency( obj, trans, false );
439 myAISContext->Redisplay( obj, Standard_False, Standard_True );
441 myV3dViewer->Update();
444 //****************************************************************
445 void OCCViewer_Viewer::toggleTrihedron()
447 setTrihedronShown( !isTrihedronVisible() );
450 bool OCCViewer_Viewer::isTrihedronVisible() const
452 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
455 void OCCViewer_Viewer::setTrihedronShown( const bool on )
457 if ( myTrihedron.IsNull() )
461 myAISContext->Display( myTrihedron );
463 myAISContext->Erase( myTrihedron );
466 int OCCViewer_Viewer::trihedronSize() const
469 if ( !myTrihedron.IsNull() )
470 sz = (int)myTrihedron->Size();
474 void OCCViewer_Viewer::setTrihedronSize( const int sz )
476 if ( !myTrihedron.IsNull() )
477 myTrihedron->SetSize( sz );
480 void OCCViewer_Viewer::setIsos( const int u, const int v )
482 Handle(AIS_InteractiveContext) ic = getAISContext();
486 ic->SetIsoNumber( u, AIS_TOI_IsoU );
487 ic->SetIsoNumber( v, AIS_TOI_IsoV );
490 void OCCViewer_Viewer::isos( int& u, int& v ) const
492 Handle(AIS_InteractiveContext) ic = getAISContext();
495 u = ic->IsoNumber( AIS_TOI_IsoU );
496 v = ic->IsoNumber( AIS_TOI_IsoV );