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"
33 #include "VTKViewer_ViewModel.h"
35 #include "SVTK_RenderWindowInteractor.h"
36 #include "SVTK_RenderWindow.h"
38 //#include <ToolsGUI.h>
40 #include <SALOME_Actor.h>
41 #include <SALOME_InteractiveObject.hxx>
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"
51 //#include "SALOMEDSClient.hxx"
52 //#include "SALOMEDS_StudyManager.hxx"
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.
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()
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 );
76 //==========================================================
77 SVTK_Viewer::SVTK_Viewer()
79 myTrihedronSize = 100;
82 //==========================================================
83 SVTK_Viewer::~SVTK_Viewer()
87 QColor SVTK_Viewer::backgroundColor() const
92 void SVTK_Viewer::setBackgroundColor( const QColor& c )
98 //==========================================================
99 SUIT_ViewWindow* SVTK_Viewer::createView( SUIT_Desktop* theDesktop )
101 SVTK_ViewWindow* vw = new SVTK_ViewWindow( theDesktop, this );
102 vw->setBackgroundColor( backgroundColor() );
103 vw->SetTrihedronSize( trihedronSize() );
107 int SVTK_Viewer::trihedronSize() const
109 return myTrihedronSize;
112 void SVTK_Viewer::setTrihedronSize( const int sz )
114 myTrihedronSize = sz;
116 SUIT_ViewManager* vm = getViewManager();
120 QPtrVector<SUIT_ViewWindow> vec = vm->getViews();
121 for ( int i = 0; i < vec.count(); i++ )
123 SUIT_ViewWindow* win = vec.at( i );
124 if ( !win || !win->inherits( "SVTK_ViewWindow" ) )
127 SVTK_ViewWindow* vw = (SVTK_ViewWindow*)win;
128 vw->SetTrihedronSize( sz );
132 //==========================================================
133 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
135 SUIT_ViewModel::setViewManager(theViewManager);
137 if ( !theViewManager )
140 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
141 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
143 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
144 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
146 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
147 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
150 //==========================================================
151 void SVTK_Viewer::contextMenuPopup( QPopupMenu* thePopup )
153 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
154 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
156 thePopup->insertSeparator();
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() ) );
163 //==========================================================
164 void SVTK_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
166 if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
167 if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
168 switch(event->button()) {
170 aRWI->LeftButtonPressed(event) ;
173 aRWI->MiddleButtonPressed(event) ;
176 aRWI->RightButtonPressed(event) ;
185 //==========================================================
188 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
190 if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
191 if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
192 aRWI->MouseMove( event );
197 //==========================================================
200 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
202 if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
203 if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
204 switch(event->button()) {
206 aRWI->LeftButtonReleased(event) ;
209 aRWI->MiddleButtonReleased(event) ;
212 aRWI->RightButtonReleased(event) ;
221 //==========================================================
224 ::enableSelection(bool isEnabled)
226 mySelectionEnabled = isEnabled;
227 //!! To be done for view windows
230 //==========================================================
233 ::enableMultiselection(bool isEnable)
235 myMultiSelectionEnabled = isEnable;
236 //!! To be done for view windows
239 void SVTK_Viewer::onDumpView()
241 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(myViewManager->getActiveView());
246 //==========================================================
247 void SVTK_Viewer::onChangeBgColor()
249 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(myViewManager->getActiveView());
251 QColor aColor = QColorDialog::getColor( aView->backgroundColor(), aView );
252 if ( aColor.isValid() )
253 aView->setBackgroundColor( aColor );
257 //==========================================================
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();
269 //==========================================================
272 ::Display( const SALOME_VTKPrs* prs )
274 // try do downcast object
275 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
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);
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() );
316 //==========================================================
319 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
321 // try do downcast object
322 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
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);
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())
349 aRWI->Remove(anAct,false);
351 aRWI->Erase(anAct,forced);
358 //==========================================================
361 ::EraseAll( const bool forced )
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)){
372 aView->unHighlightAll();
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);
387 aRenderer->RemoveActor(anAct);
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();
407 //==========================================================
410 ::CreatePrs( const char* entry )
412 SVTK_Prs* prs = new SVTK_Prs();
414 vtkRenderer* rnr = ( (SVTK_ViewWindow*) getViewManager()->getActiveView() )->getRenderer();
415 vtkActorCollection* theActors = rnr->GetActors();
416 theActors->InitTraversal();
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 );
428 //==========================================================
431 ::BeforeDisplay( SALOME_Displayer* d )
433 d->BeforeDisplay( this, SALOME_VTKViewType() );
436 //==========================================================
438 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
440 d->AfterDisplay( this, SALOME_VTKViewType() );
443 //==========================================================
446 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
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 ))
458 //==========================================================
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())
474 ::onSelectionChanged()
476 emit selectionChanged();