1 #include <qpopupmenu.h>
2 #include <qcolordialog.h>
5 #include <vtkRenderer.h>
6 #include <vtkActorCollection.h>
8 #include "SUIT_Session.h"
10 #include "SVTK_Selection.h"
11 #include "SVTK_ViewModel.h"
12 #include "SVTK_ViewWindow.h"
15 #include "SVTK_RenderWindowInteractor.h"
16 #include "SVTK_RenderWindow.h"
20 #include "SALOME_Actor.h"
21 #include "SALOME_InteractiveObject.hxx"
23 #include "Utils_ORB_INIT.hxx"
24 #include "Utils_SINGLETON.hxx"
25 #include "SALOME_ModuleCatalog_impl.hxx"
26 #include "SALOME_NamingService.hxx"
28 #include "SALOMEDSClient.hxx"
29 #include "SALOMEDS_StudyManager.hxx"
31 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
32 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
33 // SALOMEDS::StudyManager - no linkage with SalomeApp.
35 static _PTR(Study) getStudyDS()
37 SALOMEDSClient_Study* aStudy = NULL;
38 _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
40 // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
41 // id of its underlying SALOMEDS::Study
42 SUIT_Application* app = SUIT_Session::session()->activeApplication();
43 if ( !app ) return _PTR(Study)(aStudy);
44 SUIT_Study* stud = app->activeStudy();
45 if ( !stud ) return _PTR(Study)(aStudy);
46 const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
47 // get SALOMEDS_Study with this id from StudyMgr
48 return aMgr->GetStudyByID( id );
51 //==========================================================
52 SVTK_Viewer::SVTK_Viewer()
56 //==========================================================
57 SVTK_Viewer::~SVTK_Viewer()
61 QColor SVTK_Viewer::backgroundColor() const
66 void SVTK_Viewer::setBackgroundColor( const QColor& c )
72 //==========================================================
73 SUIT_ViewWindow* SVTK_Viewer::createView( SUIT_Desktop* theDesktop )
75 SVTK_ViewWindow* vw = new SVTK_ViewWindow( theDesktop, this );
76 vw->setBackgroundColor( backgroundColor() );
80 //==========================================================
81 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
83 SUIT_ViewModel::setViewManager(theViewManager);
88 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
89 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
91 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
92 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
94 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
95 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
98 //==========================================================
99 void SVTK_Viewer::contextMenuPopup( QPopupMenu* thePopup )
101 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
102 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
104 thePopup->insertSeparator();
106 SVTK_ViewWindow* aView = (SVTK_ViewWindow*)(myViewManager->getActiveView());
107 if ( aView && !aView->getToolBar()->isVisible() )
108 thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
111 //==========================================================
112 void SVTK_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
114 if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
115 if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
116 switch(event->button()) {
118 aRWI->LeftButtonPressed(event) ;
121 aRWI->MiddleButtonPressed(event) ;
124 aRWI->RightButtonPressed(event) ;
133 //==========================================================
136 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
138 if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
139 if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
140 aRWI->MouseMove( event );
145 //==========================================================
148 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
150 if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
151 if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
152 switch(event->button()) {
154 aRWI->LeftButtonReleased(event) ;
157 aRWI->MiddleButtonReleased(event) ;
160 aRWI->RightButtonReleased(event) ;
169 //==========================================================
172 ::enableSelection(bool isEnabled)
174 mySelectionEnabled = isEnabled;
175 //!! To be done for view windows
178 //==========================================================
181 ::enableMultiselection(bool isEnable)
183 myMultiSelectionEnabled = isEnable;
184 //!! To be done for view windows
187 void SVTK_Viewer::onDumpView()
189 SVTK_ViewWindow* aView = (SVTK_ViewWindow*)(myViewManager->getActiveView());
194 //==========================================================
195 void SVTK_Viewer::onChangeBgColor()
197 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
198 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
199 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
200 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
201 QColor aColor = QColorDialog::getColor( aView->backgroundColor(), aView);
202 if ( aColor.isValid() )
203 aView->setBackgroundColor(aColor);
207 //==========================================================
212 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
213 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
214 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
215 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
216 aView->getToolBar()->show();
219 //==========================================================
222 ::Display( const SALOME_VTKPrs* prs )
224 // try do downcast object
225 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
228 if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
229 // get SALOMEDS Study
230 _PTR(Study) aStudy(getStudyDS());
231 anActorCollection->InitTraversal();
232 while(vtkActor* anActor = anActorCollection->GetNextActor()){
233 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
234 // Set visibility flag
235 Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
236 if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
237 ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
239 // just display the object
240 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
241 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
242 if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
243 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
244 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor()){
245 aRWI->Display(anAct,false);
246 if(anAct->IsSetCamera()){
247 vtkRenderer* aRenderer = aView->getRenderer();
248 anAct->SetCamera( aRenderer->GetActiveCamera() );
260 //==========================================================
263 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
265 // try do downcast object
266 if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
269 if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
270 // get SALOMEDS Study
271 _PTR(Study) aStudy(getStudyDS());
272 anActorCollection->InitTraversal();
273 while(vtkActor* anActor = anActorCollection->GetNextActor())
274 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
275 // Set visibility flag
276 Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
277 if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
278 ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
280 // just display the object
281 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
282 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
283 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
284 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
285 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
287 aRWI->Remove(anAct,false);
289 aRWI->Erase(anAct,forced);
296 //==========================================================
299 ::EraseAll( const bool forced )
301 _PTR(Study) aStudy(getStudyDS());
302 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
303 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
304 if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
305 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
306 vtkRenderer* aRenderer = aView->getRenderer();
307 vtkActorCollection* anActorCollection = aRenderer->GetActors();
308 anActorCollection->InitTraversal();
309 while(vtkActor* anActor = anActorCollection->GetNextActor()){
310 if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
311 // Set visibility flag
312 Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
313 if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
314 ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
316 aRenderer->RemoveActor(anAct);
319 anAct->SetVisibility( false );
320 // erase dependent actors
321 vtkActorCollection* aCollection = vtkActorCollection::New();
322 anAct->GetChildActors( aCollection );
323 aCollection->InitTraversal();
324 while(vtkActor* aSubAct = aCollection->GetNextActor())
325 aSubAct->SetVisibility( false );
326 aCollection->Delete();
336 //==========================================================
339 ::CreatePrs( const char* entry )
341 SVTK_Prs* prs = new SVTK_Prs();
343 vtkRenderer* rnr = ( (SVTK_ViewWindow*) getViewManager()->getActiveView() )->getRenderer();
344 vtkActorCollection* theActors = rnr->GetActors();
345 theActors->InitTraversal();
347 while( ( ac = theActors->GetNextActor() ) ) {
348 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
349 if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
350 prs->AddObject( ac );
357 //==========================================================
360 ::BeforeDisplay( SALOME_Displayer* d )
362 d->BeforeDisplay( this, SALOME_VTKViewType() );
365 //==========================================================
367 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
369 d->AfterDisplay( this, SALOME_VTKViewType() );
372 //==========================================================
375 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
377 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
378 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
379 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
380 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
381 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
382 if(!aRWI->isVisible( io ))
387 //==========================================================
392 // if (theUpdateTrihedron) onAdjustTrihedron();
393 QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
394 for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
395 if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
396 if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
397 if(SVTK_RenderWindow* aRW = aView->getRenderWindow())
403 ::onSelectionChanged()
405 emit selectionChanged();