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