]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_ViewModel.cxx
Salome HOME
Merge from OCC_development_generic_2006
[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_View.h"
32 #include "SVTK_MainWindow.h"
33 #include "SVTK_Prs.h"
34
35 #include "VTKViewer_ViewModel.h"
36
37 #include <SALOME_Actor.h>
38 #include <SALOME_InteractiveObject.hxx>
39
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. 
43
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() 
48 //{
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 );
60 //}
61
62 //==========================================================
63 SVTK_Viewer::SVTK_Viewer()
64 {
65   myTrihedronSize = 105;
66 }
67
68 //==========================================================
69 SVTK_Viewer::~SVTK_Viewer() 
70 {
71 }
72
73 QColor
74 SVTK_Viewer
75 ::backgroundColor() const
76 {
77   return myBgColor;
78 }
79
80 void
81 SVTK_Viewer
82 ::setBackgroundColor( const QColor& theColor )
83 {
84   if ( !theColor.isValid() )
85     return;
86
87   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
88   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
89     if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
90       if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViewWindow)){
91         aView->getMainWindow()->SetBackgroundColor(theColor);
92       }
93     }
94   }
95
96   myBgColor = theColor;
97 }
98
99 //==========================================================
100 SUIT_ViewWindow* 
101 SVTK_Viewer::
102 createView( SUIT_Desktop* theDesktop )
103 {
104   SVTK_ViewWindow* aViewWindow = new SVTK_ViewWindow(theDesktop);
105   aViewWindow->Initialize(this);
106     
107   aViewWindow->setBackgroundColor( backgroundColor() );
108   aViewWindow->SetTrihedronSize( trihedronSize() );
109
110   return aViewWindow;
111 }
112
113 int SVTK_Viewer::trihedronSize() const
114 {
115   return myTrihedronSize;
116 }
117
118 void SVTK_Viewer::setTrihedronSize( const int sz )
119 {
120   myTrihedronSize = sz;
121
122   SUIT_ViewManager* vm = getViewManager();
123   if ( !vm )
124     return;
125
126   QPtrVector<SUIT_ViewWindow> vec = vm->getViews();
127   for ( int i = 0; i < vec.count(); i++ )
128   {
129     SUIT_ViewWindow* win = vec.at( i );
130     if ( !win || !win->inherits( "SVTK_ViewWindow" ) )
131       continue;
132
133     SVTK_ViewWindow* vw = (SVTK_ViewWindow*)win;
134     vw->SetTrihedronSize( sz );
135   }
136 }
137
138 //==========================================================
139 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
140 {
141   SUIT_ViewModel::setViewManager(theViewManager);
142
143   if ( !theViewManager )
144     return;
145
146   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
147           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
148   
149   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
150           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
151   
152   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
153           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
154 }
155
156 //==========================================================
157 void
158 SVTK_Viewer
159 ::contextMenuPopup( QPopupMenu* thePopup )
160 {
161   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
162   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
163
164   thePopup->insertSeparator();
165
166   if(TViewWindow* aView = dynamic_cast<TViewWindow*>(myViewManager->getActiveView())){
167     if ( !aView->getView()->GetMainWindow()->getToolBar()->isVisible() ){
168       thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
169     }
170   }
171 }
172
173 //==========================================================
174 void 
175 SVTK_Viewer
176 ::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
177 {}
178
179 //==========================================================
180 void 
181 SVTK_Viewer
182 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
183 {}
184
185 //==========================================================
186 void 
187 SVTK_Viewer
188 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
189 {}
190
191 //==========================================================
192 void 
193 SVTK_Viewer
194 ::enableSelection(bool isEnabled)
195 {
196   mySelectionEnabled = isEnabled;
197   //!! To be done for view windows
198 }
199
200 //==========================================================
201 void
202 SVTK_Viewer
203 ::enableMultiselection(bool isEnable)
204 {
205   myMultiSelectionEnabled = isEnable;
206   //!! To be done for view windows
207 }
208
209 void
210 SVTK_Viewer
211 ::onDumpView()
212 {
213   if(SUIT_ViewWindow* aView = myViewManager->getActiveView())
214     aView->onDumpView();
215 }
216
217 //==========================================================
218 void
219 SVTK_Viewer
220 ::onChangeBgColor()
221 {
222   if(SUIT_ViewWindow* aView = myViewManager->getActiveView()){
223     QColor aColor = QColorDialog::getColor( backgroundColor(), aView);
224     setBackgroundColor(aColor);
225   }
226 }
227
228 //==========================================================
229 void
230 SVTK_Viewer
231 ::onShowToolbar() 
232 {
233   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
234   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
235     if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at(i))){
236       aView->getView()->GetMainWindow()->getToolBar()->show();
237     }
238   }
239 }
240
241 //==========================================================
242 void
243 SVTK_Viewer
244 ::Display( const SALOME_VTKPrs* prs )
245 {
246   // try do downcast object
247   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
248     if(aPrs->IsNull())
249       return;
250     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
251       // get SALOMEDS Study
252       // Temporarily commented to avoid awful dependecy on SALOMEDS
253       // TODO: better mechanism of storing display/erse status in a study
254       // should be provided...
255       // _PTR(Study) aStudy(getStudyDS());
256       anActorCollection->InitTraversal();
257       while(vtkActor* anActor = anActorCollection->GetNextActor()){
258         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
259           // Set visibility flag
260           // Temporarily commented to avoid awful dependecy on SALOMEDS
261           // TODO: better mechanism of storing display/erse status in a study
262           // should be provided...
263           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
264           //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
265           //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
266           //}
267           // just display the object
268           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
269           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
270             if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i))){
271               if(SVTK_View* aView = aViewWindow->getView()){
272                 aView->Display(anAct,false);
273                 if(anAct->IsSetCamera()){
274                   vtkRenderer* aRenderer = aView->getRenderer();
275                   anAct->SetCamera( aRenderer->GetActiveCamera() );
276                 }
277               }
278             }
279           }
280         }
281       }
282     }
283   }
284 }
285
286 //==========================================================
287 void
288 SVTK_Viewer
289 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
290 {
291   // try do downcast object
292   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
293     if(aPrs->IsNull())
294       return;
295     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
296       // get SALOMEDS Study
297       // Temporarily commented to avoid awful dependecy on SALOMEDS
298       // TODO: better mechanism of storing display/erse status in a study
299       // should be provided...
300       //_PTR(Study) aStudy(getStudyDS());
301       anActorCollection->InitTraversal();
302       while(vtkActor* anActor = anActorCollection->GetNextActor())
303         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
304           // Set visibility flag
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           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
309           //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
310           //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
311           //}
312           // just display the object
313           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
314           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
315             if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
316               if(SVTK_View* aView = aViewWindow->getView())
317                 if ( forced )
318                   aView->Remove(anAct,false);
319                 else
320                   aView->Erase(anAct,forced);
321           }
322         }
323     }
324   }
325 }
326   
327 //==========================================================
328 void
329 SVTK_Viewer
330 ::EraseAll( const bool forced )
331 {
332   // Temporarily commented to avoid awful dependecy on SALOMEDS
333   // TODO: better mechanism of storing display/erse status in a study
334   // should be provided...
335   //_PTR(Study) aStudy(getStudyDS());
336   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
337   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
338     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
339       if(SVTK_View* aView = aViewWindow->getView()){
340         vtkRenderer* aRenderer =  aView->getRenderer();
341         vtkActorCollection* anActorCollection = aRenderer->GetActors();
342         anActorCollection->InitTraversal();
343         while(vtkActor* anActor = anActorCollection->GetNextActor()){
344           if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
345             // Set visibility flag
346             // Temporarily commented to avoid awful dependecy on SALOMEDS
347             // TODO: better mechanism of storing display/erse status in a study
348             // should be provided...
349             //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
350             //if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
351             //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
352             if(forced)
353               aRenderer->RemoveActor(anAct);
354             else{
355               // just erase actor
356               anAct->SetVisibility( false );
357               // erase dependent actors
358               vtkActorCollection* aCollection = vtkActorCollection::New();
359               anAct->GetChildActors( aCollection );
360               aCollection->InitTraversal();
361               while(vtkActor* aSubAct = aCollection->GetNextActor())
362                 aSubAct->SetVisibility( false );
363               aCollection->Delete();
364             }
365           }
366         }
367       }
368   }
369   Repaint();
370 }
371
372 //==========================================================
373 SALOME_Prs* 
374 SVTK_Viewer
375 ::CreatePrs( const char* entry )
376 {
377   SVTK_Prs* prs = new SVTK_Prs();
378   if ( entry ) {
379     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(getViewManager()->getActiveView()))
380       if(SVTK_View* aView = aViewWindow->getView()){
381         vtkRenderer* aRenderer =  aView->getRenderer();
382         vtkActorCollection* theActors = aRenderer->GetActors();
383         theActors->InitTraversal();
384         vtkActor* ac;
385         while( ( ac = theActors->GetNextActor() ) ) {
386           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
387           if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
388             prs->AddObject( ac );
389           }
390         }
391       }
392   }
393   return prs;
394 }
395
396 //==========================================================
397 void
398 SVTK_Viewer
399 ::BeforeDisplay( SALOME_Displayer* d )
400 {
401   d->BeforeDisplay( this, SALOME_VTKViewType() );
402 }
403
404 //==========================================================
405 void
406 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
407 {
408   d->AfterDisplay( this, SALOME_VTKViewType() );
409 }
410
411 //==========================================================
412 bool
413 SVTK_Viewer
414 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
415 {
416   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
417   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
418     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
419       if(SVTK_View* aView = aViewWindow->getView())
420         if(!aView->isVisible( io ))
421           return false;
422
423   return true;
424 }
425
426 //==========================================================
427 void 
428 SVTK_Viewer
429 ::Repaint()
430 {
431 //  if (theUpdateTrihedron) onAdjustTrihedron();
432   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
433   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
434     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
435       if(SVTK_View* aView = aViewWindow->getView())
436         aView->Repaint();
437 }