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
19 #include <qpopupmenu.h>
20 #include <qcolordialog.h>
22 #include <vtkCamera.h>
23 #include <vtkRenderer.h>
24 #include <vtkActorCollection.h>
26 #include "SUIT_Session.h"
28 #include "SVTK_Selection.h"
29 #include "SVTK_ViewModel.h"
30 #include "SVTK_ViewWindow.h"
31 #include "SVTK_View.h"
32 #include "SVTK_MainWindow.h"
35 #include "VTKViewer_ViewModel.h"
37 #include <SALOME_Actor.h>
38 #include <SALOME_InteractiveObject.hxx>
40 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
41 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
42 // SALOMEDS::StudyManager - no linkage with SalomeApp.
44 // Temporarily commented to avoid awful dependecy on SALOMEDS
45 // TODO: better mechanism of storing display/erse status in a study
46 // should be provided...
47 //static _PTR(Study) getStudyDS()
49 // SALOMEDSClient_Study* aStudy = NULL;
50 // _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
51 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
52 // id of its underlying SALOMEDS::Study
53 // SUIT_Application* app = SUIT_Session::session()->activeApplication();
54 // if ( !app ) return _PTR(Study)(aStudy);
55 // SUIT_Study* stud = app->activeStudy();
56 // if ( !stud ) return _PTR(Study)(aStudy);
57 // const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
58 // get SALOMEDS_Study with this id from StudyMgr
59 // return aMgr->GetStudyByID( id );
65 SVTK_Viewer::SVTK_Viewer()
67 myTrihedronSize = 105;
68 myTrihedronRelative = true;
74 SVTK_Viewer::~SVTK_Viewer()
79 \return background color
83 ::backgroundColor() const
89 Changes background color
90 \param theColor - new background color
94 ::setBackgroundColor( const QColor& theColor )
96 if ( !theColor.isValid() )
99 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
100 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
101 if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
102 if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViewWindow)){
103 aView->setBackgroundColor(theColor);
108 myBgColor = theColor;
111 /*!Create new instance of view window on desktop \a theDesktop.
112 *\retval SUIT_ViewWindow* - created view window pointer.
116 createView( SUIT_Desktop* theDesktop )
118 TViewWindow* aViewWindow = new TViewWindow(theDesktop);
119 aViewWindow->Initialize(this);
121 aViewWindow->setBackgroundColor( backgroundColor() );
122 aViewWindow->SetTrihedronSize( trihedronSize(), trihedronRelative() );
128 \return trihedron size
130 vtkFloatingPointType SVTK_Viewer::trihedronSize() const
132 return myTrihedronSize;
136 \return true if thihedron changes size in accordance with bounding box
138 bool SVTK_Viewer::trihedronRelative() const
140 return myTrihedronRelative;
144 Sets trihedron size and relativeness( whether thihedron changes size in accordance with bounding box)
145 \param theSize - new size
146 \param theRelative - new relativeness
148 void SVTK_Viewer::setTrihedronSize( const vtkFloatingPointType theSize, const bool theRelative )
150 myTrihedronSize = theSize;
151 myTrihedronRelative = theRelative;
153 if (SUIT_ViewManager* aViewManager = getViewManager()) {
154 QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
155 for ( uint i = 0; i < aViews.count(); i++ )
157 if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
158 aView->SetTrihedronSize( theSize, theRelative );
164 Sets new view manager
165 \param theViewManager - new view manager
167 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
169 SUIT_ViewModel::setViewManager(theViewManager);
171 if ( !theViewManager )
174 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
175 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
177 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
178 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
180 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
181 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
185 Builds popup for vtk viewer
189 ::contextMenuPopup( QPopupMenu* thePopup )
191 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
192 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
194 thePopup->insertSeparator();
196 if(TViewWindow* aView = dynamic_cast<TViewWindow*>(myViewManager->getActiveView())){
197 if ( !aView->getMainWindow()->getToolBar()->isVisible() ){
198 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
200 aView->RefreshDumpImage();
205 SLOT: called on mouse button press, empty implementation
209 ::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
213 SLOT: called on mouse move, empty implementation
217 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
221 SLOT: called on mouse button release, empty implementation
225 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
229 Enables/disables selection
230 \param isEnabled - new state
234 ::enableSelection(bool isEnabled)
236 mySelectionEnabled = isEnabled;
237 //!! To be done for view windows
241 Enables/disables selection of many object
242 \param isEnabled - new state
246 ::enableMultiselection(bool isEnable)
248 myMultiSelectionEnabled = isEnable;
249 //!! To be done for view windows
253 SLOT: called on dump view operation is activated, stores scene to raster file
259 if(SUIT_ViewWindow* aView = myViewManager->getActiveView())
264 SLOT: called if background color is to be changed changed, passes new color to view port
270 if(SUIT_ViewWindow* aView = myViewManager->getActiveView()){
271 QColor aColor = QColorDialog::getColor( backgroundColor(), aView);
272 setBackgroundColor(aColor);
277 SLOT: called when popup item "Show toolbar" is activated, shows toolbar of active view window
283 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
284 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
285 if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at(i))){
286 aView->getMainWindow()->getToolBar()->show();
293 \param prs - presentation
297 ::Display( const SALOME_VTKPrs* prs )
299 // try do downcast object
300 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
303 if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
304 // get SALOMEDS Study
305 // Temporarily commented to avoid awful dependecy on SALOMEDS
306 // TODO: better mechanism of storing display/erse status in a study
307 // should be provided...
308 // _PTR(Study) aStudy(getStudyDS());
309 anActorCollection->InitTraversal();
310 while(vtkActor* anActor = anActorCollection->GetNextActor()){
311 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
312 // Set visibility flag
313 // Temporarily commented to avoid awful dependecy on SALOMEDS
314 // TODO: better mechanism of storing display/erse status in a study
315 // should be provided...
316 //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
317 //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
318 // ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
320 // just display the object
321 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
322 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
323 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i))){
324 if(SVTK_View* aView = aViewWindow->getView()){
325 aView->Display(anAct,false);
326 if(anAct->IsSetCamera()){
327 vtkRenderer* aRenderer = aView->getRenderer();
328 anAct->SetCamera( aRenderer->GetActiveCamera() );
341 \param prs - presentation
342 \param forced - removes object from view
346 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
348 // try do downcast object
349 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
352 if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
353 // get SALOMEDS Study
354 // Temporarily commented to avoid awful dependecy on SALOMEDS
355 // TODO: better mechanism of storing display/erse status in a study
356 // should be provided...
357 //_PTR(Study) aStudy(getStudyDS());
358 anActorCollection->InitTraversal();
359 while(vtkActor* anActor = anActorCollection->GetNextActor())
360 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
361 // Set visibility flag
362 // Temporarily commented to avoid awful dependecy on SALOMEDS
363 // TODO: better mechanism of storing display/erse status in a study
364 // should be provided...
365 //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
366 //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
367 // ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
369 // just display the object
370 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
371 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
372 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
373 if(SVTK_View* aView = aViewWindow->getView())
375 aView->Remove(anAct,false);
377 aView->Erase(anAct,forced);
385 Erase all presentations
386 \param forced - removes all objects from view
390 ::EraseAll( const bool forced )
392 // Temporarily commented to avoid awful dependecy on SALOMEDS
393 // TODO: better mechanism of storing display/erse status in a study
394 // should be provided...
395 //_PTR(Study) aStudy(getStudyDS());
396 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
397 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
398 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
399 if(SVTK_View* aView = aViewWindow->getView()){
400 vtkRenderer* aRenderer = aView->getRenderer();
401 vtkActorCollection* anActorCollection = aRenderer->GetActors();
402 anActorCollection->InitTraversal();
403 while(vtkActor* anActor = anActorCollection->GetNextActor()){
404 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
405 // Set visibility flag
406 // Temporarily commented to avoid awful dependecy on SALOMEDS
407 // TODO: better mechanism of storing display/erse status in a study
408 // should be provided...
409 //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
410 //if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
411 // ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
413 aRenderer->RemoveActor(anAct);
416 anAct->SetVisibility( false );
417 // erase dependent actors
418 vtkActorCollection* aCollection = vtkActorCollection::New();
419 anAct->GetChildActors( aCollection );
420 aCollection->InitTraversal();
421 while(vtkActor* aSubAct = aCollection->GetNextActor())
422 aSubAct->SetVisibility( false );
423 aCollection->Delete();
433 Create presentation corresponding to the entry
438 ::CreatePrs( const char* entry )
440 SVTK_Prs* prs = new SVTK_Prs();
442 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(getViewManager()->getActiveView()))
443 if(SVTK_View* aView = aViewWindow->getView()){
444 vtkRenderer* aRenderer = aView->getRenderer();
445 vtkActorCollection* theActors = aRenderer->GetActors();
446 theActors->InitTraversal();
448 while( ( ac = theActors->GetNextActor() ) ) {
449 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
450 if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
451 prs->AddObject( ac );
460 Auxiliary method called before displaying of objects
464 ::BeforeDisplay( SALOME_Displayer* d )
466 d->BeforeDisplay( this, SALOME_VTKViewType() );
470 Auxiliary method called after displaying of objects
473 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
475 d->AfterDisplay( this, SALOME_VTKViewType() );
479 \return true if object is displayed in viewer
480 \param obj - object to be checked
484 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
486 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
487 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
488 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
489 if(TViewWindow* aViewWnd = dynamic_cast<TViewWindow*>(aViewWindow))
490 if(SVTK_View* aView = aViewWnd->getView())
491 if(!aView->isVisible( io ))
498 Updates current viewer
504 // if (theUpdateTrihedron) onAdjustTrihedron();
505 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
506 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
507 if(TViewWindow* aViewWindow = dynamic_cast<TViewWindow*>(aViews.at(i)))
508 if(SVTK_View* aView = aViewWindow->getView())