]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_ViewModel.cxx
Salome HOME
Join modifications from branch BR_3_1_0deb
[modules/gui.git] / src / SVTK / SVTK_ViewModel.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include <qpopupmenu.h>
20 #include <qcolordialog.h>
21
22 #include <vtkCamera.h>
23 #include <vtkRenderer.h>
24 #include <vtkActorCollection.h>
25
26 #include "SUIT_Session.h"
27
28 #include "SVTK_Selection.h"
29 #include "SVTK_ViewModel.h"
30 #include "SVTK_ViewWindow.h"
31 #include "SVTK_Prs.h"
32
33 #include "VTKViewer_ViewModel.h"
34
35 #include "SVTK_RenderWindowInteractor.h"
36 #include "SVTK_RenderWindow.h"
37
38 //#include <ToolsGUI.h>
39
40 #include <SALOME_Actor.h>
41 #include <SALOME_InteractiveObject.hxx>
42
43 // Temporarily commented to avoid awful dependecy on SALOMEDS
44 // TODO: better mechanism of storing display/erse status in a study
45 // should be provided...
46 //#include "Utils_ORB_INIT.hxx"
47 //#include "Utils_SINGLETON.hxx"
48 //#include "SALOME_ModuleCatalog_impl.hxx"
49 //#include "SALOME_NamingService.hxx"
50
51 //#include "SALOMEDSClient.hxx"
52 //#include "SALOMEDS_StudyManager.hxx"
53
54 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
55 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from 
56 // SALOMEDS::StudyManager - no linkage with SalomeApp. 
57
58 // Temporarily commented to avoid awful dependecy on SALOMEDS
59 // TODO: better mechanism of storing display/erse status in a study
60 // should be provided...
61 //static _PTR(Study) getStudyDS() 
62 //{
63 //  SALOMEDSClient_Study* aStudy = NULL;
64 //  _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
65   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
66   //    id of its underlying SALOMEDS::Study
67 //  SUIT_Application* app = SUIT_Session::session()->activeApplication();
68 //  if ( !app )  return _PTR(Study)(aStudy); 
69 //  SUIT_Study* stud = app->activeStudy();
70 //  if ( !stud ) return _PTR(Study)(aStudy);  
71 //  const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
72   // get SALOMEDS_Study with this id from StudyMgr
73 //  return aMgr->GetStudyByID( id );
74 //}
75
76 //==========================================================
77 SVTK_Viewer::SVTK_Viewer()
78 {
79   myTrihedronSize = 100;
80 }
81
82 //==========================================================
83 SVTK_Viewer::~SVTK_Viewer() 
84 {
85 }
86
87 QColor SVTK_Viewer::backgroundColor() const
88 {
89   return myBgColor;
90 }
91
92 void SVTK_Viewer::setBackgroundColor( const QColor& c )
93 {
94   if ( c.isValid() )
95     myBgColor = c;
96 }
97
98 //==========================================================
99 SUIT_ViewWindow* SVTK_Viewer::createView( SUIT_Desktop* theDesktop )
100 {
101   SVTK_ViewWindow* vw = new SVTK_ViewWindow( theDesktop, this );
102   vw->setBackgroundColor( backgroundColor() );
103   vw->SetTrihedronSize( trihedronSize() );
104   return vw;
105 }
106
107 int SVTK_Viewer::trihedronSize() const
108 {
109   return myTrihedronSize;
110 }
111
112 void SVTK_Viewer::setTrihedronSize( const int sz )
113 {
114   myTrihedronSize = sz;
115
116   SUIT_ViewManager* vm = getViewManager();
117   if ( !vm )
118     return;
119
120   QPtrVector<SUIT_ViewWindow> vec = vm->getViews();
121   for ( int i = 0; i < vec.count(); i++ )
122   {
123     SUIT_ViewWindow* win = vec.at( i );
124     if ( !win || !win->inherits( "SVTK_ViewWindow" ) )
125       continue;
126
127     SVTK_ViewWindow* vw = (SVTK_ViewWindow*)win;
128     vw->SetTrihedronSize( sz );
129   }
130 }
131
132 //==========================================================
133 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
134 {
135   SUIT_ViewModel::setViewManager(theViewManager);
136
137   if ( !theViewManager )
138     return;
139
140   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
141           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
142   
143   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
144           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
145   
146   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
147           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
148 }
149
150 //==========================================================
151 void SVTK_Viewer::contextMenuPopup( QPopupMenu* thePopup )
152 {
153   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
154   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
155
156   thePopup->insertSeparator();
157
158   SVTK_ViewWindow* aView = (SVTK_ViewWindow*)(myViewManager->getActiveView());
159   if ( aView && !aView->getToolBar()->isVisible() )
160     thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
161 }
162
163 //==========================================================
164 void SVTK_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
165 {
166   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
167     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
168       switch(event->button()) {
169       case LeftButton:
170         aRWI->LeftButtonPressed(event) ;
171         break ;
172       case MidButton:
173         aRWI->MiddleButtonPressed(event) ;
174         break ;
175       case RightButton:
176         aRWI->RightButtonPressed(event) ;
177         break;
178       default:
179         break ;
180       }
181     }
182   }
183 }
184
185 //==========================================================
186 void 
187 SVTK_Viewer
188 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
189 {
190   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
191     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
192       aRWI->MouseMove( event );
193     }
194   }
195 }
196
197 //==========================================================
198 void 
199 SVTK_Viewer
200 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
201 {
202   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
203     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
204       switch(event->button()) {
205       case LeftButton:
206         aRWI->LeftButtonReleased(event) ;
207         break ;
208       case MidButton:
209         aRWI->MiddleButtonReleased(event) ;
210         break ;
211       case RightButton:
212         aRWI->RightButtonReleased(event) ;
213         break;
214       default:
215         break ;
216       }
217     }
218   }
219 }
220
221 //==========================================================
222 void 
223 SVTK_Viewer
224 ::enableSelection(bool isEnabled)
225 {
226   mySelectionEnabled = isEnabled;
227   //!! To be done for view windows
228 }
229
230 //==========================================================
231 void
232 SVTK_Viewer
233 ::enableMultiselection(bool isEnable)
234 {
235   myMultiSelectionEnabled = isEnable;
236   //!! To be done for view windows
237 }
238
239 void SVTK_Viewer::onDumpView()
240 {
241   SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(myViewManager->getActiveView());
242   if ( aView )
243     aView->onDumpView();
244 }
245
246 //==========================================================
247 void SVTK_Viewer::onChangeBgColor()
248 {
249   SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(myViewManager->getActiveView());
250   if ( aView ) {
251     QColor aColor = QColorDialog::getColor( aView->backgroundColor(), aView );
252     if ( aColor.isValid() )
253       aView->setBackgroundColor( aColor );
254   }
255 }
256
257 //==========================================================
258 void
259 SVTK_Viewer
260 ::onShowToolbar() 
261 {
262   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
263   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
264     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
265       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
266         aView->getToolBar()->show();    
267 }
268
269 //==========================================================
270 void
271 SVTK_Viewer
272 ::Display( const SALOME_VTKPrs* prs )
273 {
274   // try do downcast object
275   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
276     if(aPrs->IsNull())
277       return;
278     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
279       // get SALOMEDS Study
280       // Temporarily commented to avoid awful dependecy on SALOMEDS
281       // TODO: better mechanism of storing display/erse status in a study
282       // should be provided...
283       // _PTR(Study) aStudy(getStudyDS());
284       anActorCollection->InitTraversal();
285       while(vtkActor* anActor = anActorCollection->GetNextActor()){
286         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
287           // Set visibility flag
288           // Temporarily commented to avoid awful dependecy on SALOMEDS
289           // TODO: better mechanism of storing display/erse status in a study
290           // should be provided...
291           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
292           //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
293           //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
294           //}
295           // just display the object
296           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
297           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
298             if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
299               if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
300                 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor()){
301                   aRWI->Display(anAct,false);
302                   if(anAct->IsSetCamera()){
303                     vtkRenderer* aRenderer =  aView->getRenderer();
304                     anAct->SetCamera( aRenderer->GetActiveCamera() );
305                   }
306                 }
307               }
308             }
309           }
310         }
311       }
312     }
313   }
314 }
315
316 //==========================================================
317 void
318 SVTK_Viewer
319 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
320 {
321   // try do downcast object
322   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
323     if(aPrs->IsNull())
324       return;
325     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
326       // get SALOMEDS Study
327       // Temporarily commented to avoid awful dependecy on SALOMEDS
328       // TODO: better mechanism of storing display/erse status in a study
329       // should be provided...
330       //_PTR(Study) aStudy(getStudyDS());
331       anActorCollection->InitTraversal();
332       while(vtkActor* anActor = anActorCollection->GetNextActor())
333         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
334           // Set visibility flag
335           // Temporarily commented to avoid awful dependecy on SALOMEDS
336           // TODO: better mechanism of storing display/erse status in a study
337           // should be provided...
338           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
339           //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
340           //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
341           //}
342           // just display the object
343           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
344           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
345             if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
346               if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
347                 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
348                   if ( forced )
349                     aRWI->Remove(anAct,false);
350                   else
351                     aRWI->Erase(anAct,forced);
352           }
353         }
354     }
355   }
356 }
357   
358 //==========================================================
359 void
360 SVTK_Viewer
361 ::EraseAll( const bool forced )
362 {
363   // Temporarily commented to avoid awful dependecy on SALOMEDS
364   // TODO: better mechanism of storing display/erse status in a study
365   // should be provided...
366   //_PTR(Study) aStudy(getStudyDS());
367   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
368   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
369     if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
370       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
371
372         aView->unHighlightAll();
373
374         vtkRenderer* aRenderer =  aView->getRenderer();
375         vtkActorCollection* anActorCollection = aRenderer->GetActors();
376         anActorCollection->InitTraversal();
377         while(vtkActor* anActor = anActorCollection->GetNextActor()){
378           if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
379             // Set visibility flag
380             // Temporarily commented to avoid awful dependecy on SALOMEDS
381             // TODO: better mechanism of storing display/erse status in a study
382             // should be provided...
383             //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
384             //if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
385             //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
386             if(forced)
387               aRenderer->RemoveActor(anAct);
388             else{
389               // just erase actor
390               anAct->SetVisibility( false );
391               // erase dependent actors
392               vtkActorCollection* aCollection = vtkActorCollection::New();
393               anAct->GetChildActors( aCollection );
394               aCollection->InitTraversal();
395               while(vtkActor* aSubAct = aCollection->GetNextActor())
396                 aSubAct->SetVisibility( false );
397               aCollection->Delete();
398             }
399           }
400         }
401       }
402     }
403   }
404   Repaint();
405 }
406
407 //==========================================================
408 SALOME_Prs* 
409 SVTK_Viewer
410 ::CreatePrs( const char* entry )
411 {
412   SVTK_Prs* prs = new SVTK_Prs();
413   if ( entry ) {
414     vtkRenderer* rnr =  ( (SVTK_ViewWindow*) getViewManager()->getActiveView() )->getRenderer();
415     vtkActorCollection* theActors = rnr->GetActors();
416     theActors->InitTraversal();
417     vtkActor* ac;
418     while( ( ac = theActors->GetNextActor() ) ) {
419       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
420       if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
421         prs->AddObject( ac );
422       }
423     }
424   }
425   return prs;
426 }
427
428 //==========================================================
429 void
430 SVTK_Viewer
431 ::BeforeDisplay( SALOME_Displayer* d )
432 {
433   d->BeforeDisplay( this, SALOME_VTKViewType() );
434 }
435
436 //==========================================================
437 void
438 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
439 {
440   d->AfterDisplay( this, SALOME_VTKViewType() );
441 }
442
443 //==========================================================
444 bool
445 SVTK_Viewer
446 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
447 {
448   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
449   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
450     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
451       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
452         if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
453           if(!aRWI->isVisible( io ))
454             return false;
455   return true;
456 }
457
458 //==========================================================
459 void 
460 SVTK_Viewer
461 ::Repaint()
462 {
463 //  if (theUpdateTrihedron) onAdjustTrihedron();
464   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
465   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
466     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
467       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
468         if(SVTK_RenderWindow* aRW = aView->getRenderWindow())
469           aRW->update();
470 }
471
472 void 
473 SVTK_Viewer
474 ::onSelectionChanged()
475 {
476   emit selectionChanged();
477 }
478