Salome HOME
ae81741444d36456e3da580800d22aa7988f491f
[modules/geom.git] / src / DependencyTree / DependencyTree_ViewModel.cxx
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "DependencyTree_ViewModel.h"
22 #include "DependencyTree_View.h"
23
24 // GUI includes
25 #include <SUIT_Session.h>
26 #include <SalomeApp_Study.h>
27 #include <LightApp_SelectionMgr.h>
28 #include <SALOME_ListIO.hxx>
29 #include <OCCViewer_ViewManager.h>
30
31 // GEOM includes
32 #include <GEOM_Displayer.h>
33 #include <GeometryGUI_Operations.h>
34
35 // QT includes
36 #include <QMenu>
37
38 DependencyTree_ViewModel::DependencyTree_ViewModel( const QString& title )
39 : GraphicsView_Viewer( title )
40 {
41 }
42
43 DependencyTree_ViewModel::DependencyTree_ViewModel( const QString& title, QWidget* w )
44 : GraphicsView_Viewer( title,  w )
45 {
46 }
47
48 DependencyTree_ViewModel::~DependencyTree_ViewModel()
49 {
50 }
51
52 //=================================================================================
53 // function : onShowSelected()
54 // purpose  : slot for showing selected objects in OCC Viewer
55 //=================================================================================
56 void DependencyTree_ViewModel::onShowSelected()
57 {
58   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
59   if ( !app ) return;
60
61   app->getViewManager( OCCViewer_Viewer::Type(), /*create=*/ true );
62
63   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
64   if ( !aSelMgr ) return;
65
66   SALOME_ListIO aSelList;
67   aSelMgr->selectedObjects(aSelList);
68
69   GEOM_Displayer disp;
70
71   OCCViewer_ViewManager* anOCCVM = ( OCCViewer_ViewManager* ) app->getViewManager( OCCViewer_Viewer::Type(), /*create=*/ true );
72
73   if ( SUIT_ViewModel* viewModel = anOCCVM->getViewModel() ) {
74     if ( SALOME_View* viewFrame = dynamic_cast<SALOME_View*>( viewModel ) ) {
75       SALOME_ListIteratorOfListIO Iter( aSelList );
76       for ( ; Iter.More(); Iter.Next() )
77         disp.Display( Iter.Value(), false, viewFrame );
78       viewFrame->Repaint();
79     }
80   }
81 }
82
83 //=================================================================================
84 // function : onShowOnlySelected()
85 // purpose  : slot for showing only selected objects in OCC Viewer
86 //=================================================================================
87 void DependencyTree_ViewModel::onShowOnlySelected()
88 {
89   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
90   if ( !app ) return;
91
92   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
93   if ( !aSelMgr ) return;
94
95   SALOME_ListIO aSelList;
96   aSelMgr->selectedObjects( aSelList );
97
98   GEOM_Displayer disp;
99
100   OCCViewer_ViewManager* anOCCVM = (OCCViewer_ViewManager*) app->getViewManager( OCCViewer_Viewer::Type(), /*create=*/ true );
101
102   if ( SUIT_ViewModel* viewModel = anOCCVM->getViewModel() ) {
103     if ( SALOME_View* viewFrame = dynamic_cast<SALOME_View*>( viewModel ) ) {
104       disp.EraseAll( true, false, viewFrame );
105       SALOME_ListIteratorOfListIO Iter( aSelList );
106       for ( ; Iter.More(); Iter.Next() )
107         disp.Display( Iter.Value(), false, viewFrame );
108       viewFrame->Repaint();
109     }
110   }
111 }
112
113 //=================================================================================
114 // function : contextMenuPopup()
115 // purpose  : process calling of context menu popup
116 //=================================================================================
117 void DependencyTree_ViewModel::contextMenuPopup( QMenu* theMenu )
118 {
119   GraphicsView_Viewer::contextMenuPopup( theMenu );
120
121   if( DependencyTree_View* viewPort = dynamic_cast<DependencyTree_View*>( getActiveViewPort() ) )
122   {
123     int aNbSelected = viewPort->nbSelected();
124     if( aNbSelected > 0 ) {
125       SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
126       GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
127       theMenu->clear();
128       theMenu->addAction( tr( "MEN_DISPLAY" ), this, SLOT( onShowSelected() ) );
129       theMenu->addAction( tr( "MEN_DISPLAY_ONLY" ), this, SLOT( onShowOnlySelected() ) );
130       theMenu->addAction( tr( "MEN_REBUILD_THE_TREE"), viewPort, SLOT( onRebuildModel() ) );
131       theMenu->addSeparator();
132       theMenu->addAction( aGeomGUI->action( GEOMOp::OpReduceStudy ) );
133     }
134   }
135 }