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/
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 int 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 int 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 (int i = 0; i < aViews.count(); i++) {
156 if (TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at(i))) {
157 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() ) );
204 SLOT: called on mouse button press, empty implementation
208 ::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
212 SLOT: called on mouse move, empty implementation
216 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
220 SLOT: called on mouse button release, empty implementation
224 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
228 Enables/disables selection
229 \param isEnabled - new state
233 ::enableSelection(bool isEnabled)
235 mySelectionEnabled = isEnabled;
236 //!! To be done for view windows
240 Enables/disables selection of many object
241 \param isEnabled - new state
245 ::enableMultiselection(bool isEnable)
247 myMultiSelectionEnabled = isEnable;
248 //!! To be done for view windows
252 SLOT: called on dump view operation is activated, stores scene to raster file
258 if(SUIT_ViewWindow* aView = myViewManager->getActiveView())
263 SLOT: called if background color is to be changed changed, passes new color to view port
269 if(SUIT_ViewWindow* aView = myViewManager->getActiveView()){
270 QColor aColor = QColorDialog::getColor( backgroundColor(), aView);
271 setBackgroundColor(aColor);
276 SLOT: called when popup item "Show toolbar" is activated, shows toolbar of active view window
282 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
283 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
284 if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at(i))){
285 aView->getMainWindow()->getToolBar()->show();
292 \param prs - presentation
296 ::Display( const SALOME_VTKPrs* prs )
298 // try do downcast object
299 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
302 if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
303 // get SALOMEDS Study
304 // Temporarily commented to avoid awful dependecy on SALOMEDS
305 // TODO: better mechanism of storing display/erse status in a study
306 // should be provided...
307 // _PTR(Study) aStudy(getStudyDS());
308 anActorCollection->InitTraversal();
309 while(vtkActor* anActor = anActorCollection->GetNextActor()){
310 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
311 // Set visibility flag
312 // Temporarily commented to avoid awful dependecy on SALOMEDS
313 // TODO: better mechanism of storing display/erse status in a study
314 // should be provided...
315 //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
316 //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
317 // ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
319 // just display the object
320 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
321 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
322 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i))){
323 if(SVTK_View* aView = aViewWindow->getView()){
324 aView->Display(anAct,false);
325 if(anAct->IsSetCamera()){
326 vtkRenderer* aRenderer = aView->getRenderer();
327 anAct->SetCamera( aRenderer->GetActiveCamera() );
340 \param prs - presentation
341 \param forced - removes object from view
345 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
347 // try do downcast object
348 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
351 if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
352 // get SALOMEDS Study
353 // Temporarily commented to avoid awful dependecy on SALOMEDS
354 // TODO: better mechanism of storing display/erse status in a study
355 // should be provided...
356 //_PTR(Study) aStudy(getStudyDS());
357 anActorCollection->InitTraversal();
358 while(vtkActor* anActor = anActorCollection->GetNextActor())
359 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
360 // Set visibility flag
361 // Temporarily commented to avoid awful dependecy on SALOMEDS
362 // TODO: better mechanism of storing display/erse status in a study
363 // should be provided...
364 //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
365 //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
366 // ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
368 // just display the object
369 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
370 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
371 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
372 if(SVTK_View* aView = aViewWindow->getView())
374 aView->Remove(anAct,false);
376 aView->Erase(anAct,forced);
384 Erase all presentations
385 \param forced - removes all objects from view
389 ::EraseAll( const bool forced )
391 // Temporarily commented to avoid awful dependecy on SALOMEDS
392 // TODO: better mechanism of storing display/erse status in a study
393 // should be provided...
394 //_PTR(Study) aStudy(getStudyDS());
395 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
396 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
397 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
398 if(SVTK_View* aView = aViewWindow->getView()){
399 vtkRenderer* aRenderer = aView->getRenderer();
400 vtkActorCollection* anActorCollection = aRenderer->GetActors();
401 anActorCollection->InitTraversal();
402 while(vtkActor* anActor = anActorCollection->GetNextActor()){
403 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
404 // Set visibility flag
405 // Temporarily commented to avoid awful dependecy on SALOMEDS
406 // TODO: better mechanism of storing display/erse status in a study
407 // should be provided...
408 //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
409 //if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
410 // ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
412 aRenderer->RemoveActor(anAct);
415 anAct->SetVisibility( false );
416 // erase dependent actors
417 vtkActorCollection* aCollection = vtkActorCollection::New();
418 anAct->GetChildActors( aCollection );
419 aCollection->InitTraversal();
420 while(vtkActor* aSubAct = aCollection->GetNextActor())
421 aSubAct->SetVisibility( false );
422 aCollection->Delete();
432 Create presentation corresponding to the entry
437 ::CreatePrs( const char* entry )
439 SVTK_Prs* prs = new SVTK_Prs();
441 if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(getViewManager()->getActiveView()))
442 if(SVTK_View* aView = aViewWindow->getView()){
443 vtkRenderer* aRenderer = aView->getRenderer();
444 vtkActorCollection* theActors = aRenderer->GetActors();
445 theActors->InitTraversal();
447 while( ( ac = theActors->GetNextActor() ) ) {
448 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
449 if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
450 prs->AddObject( ac );
459 Auxiliary method called before displaying of objects
463 ::BeforeDisplay( SALOME_Displayer* d )
465 d->BeforeDisplay( this, SALOME_VTKViewType() );
469 Auxiliary method called after displaying of objects
472 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
474 d->AfterDisplay( this, SALOME_VTKViewType() );
478 \return true if object is displayed in viewer
479 \param obj - object to be checked
483 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
485 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
486 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
487 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
488 if(TViewWindow* aViewWnd = dynamic_cast<TViewWindow*>(aViewWindow))
489 if(SVTK_View* aView = aViewWnd->getView())
490 if(!aView->isVisible( io ))
497 Updates current viewer
503 // if (theUpdateTrihedron) onAdjustTrihedron();
504 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
505 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
506 if(TViewWindow* aViewWindow = dynamic_cast<TViewWindow*>(aViews.at(i)))
507 if(SVTK_View* aView = aViewWindow->getView())