Salome HOME
4a497c68b22589f2325d4d3c3457c3c44527ba25
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SelectionOp.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 //
20 //
21 //
22 //  File   : SMESHGUI_SelectionOp.cxx
23 //  Author : Alexander SOLOVYOV
24 //  Module : SMESH
25
26 #include <SMESHGUI_SelectionOp.h>
27 #include <SUIT_SelectionFilter.h>
28 #include <SalomeApp_SelectionMgr.h>
29
30 /*
31   Class       : SMESHGUI_SelectionOp
32   Description : Base operation for all operations using object selection in viewer or objectbrowser
33                 through common widgets created by SalomeApp_Dialog::createObject
34 */
35
36 //=================================================================================
37 // name     : SMESHGUI_SelectionOp
38 // purpose  : 
39 //=================================================================================
40 SMESHGUI_SelectionOp::SMESHGUI_SelectionOp( const Selection_Mode mode )
41 : SMESHGUI_Operation(),
42   myDefSelectionMode( mode )
43 {
44 }
45
46 //=================================================================================
47 // name     : ~SMESHGUI_SelectionOp
48 // purpose  :
49 //=================================================================================
50 SMESHGUI_SelectionOp::~SMESHGUI_SelectionOp()
51 {
52   Filters::const_iterator anIt = myFilters.begin(),
53                           aLast = myFilters.end();
54   for( ; anIt!=aLast; anIt++ )
55     if( anIt.data() )
56       delete anIt.data();
57 }
58
59 //=================================================================================
60 // name     : startOperation
61 // purpose  :
62 //=================================================================================
63 void SMESHGUI_SelectionOp::startOperation()
64 {
65   SMESHGUI_Operation::startOperation();
66   if( dlg() )
67   {
68     disconnect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
69     disconnect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
70     disconnect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
71     connect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
72     connect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
73     connect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
74   }
75
76   myOldSelectionMode = selectionMode();
77   setSelectionMode( myDefSelectionMode );
78 }
79
80 //=================================================================================
81 // name     : removeCustomFilters
82 // purpose  :
83 //=================================================================================
84 void SMESHGUI_SelectionOp::removeCustomFilters() const
85 {
86   SalomeApp_SelectionMgr* mgr = selectionMgr();
87   if( !mgr )
88     return;
89     
90   Filters::const_iterator anIt = myFilters.begin(),
91                           aLast = myFilters.end();
92   for( ; anIt!=aLast; anIt++ )
93     if( anIt.data() )
94       mgr->removeFilter( anIt.data() );
95 }
96
97 //=================================================================================
98 // name     : commitOperation
99 // purpose  :
100 //=================================================================================
101 void SMESHGUI_SelectionOp::commitOperation()
102 {
103   removeCustomFilters();
104   setSelectionMode( myOldSelectionMode );
105   SMESHGUI_Operation::commitOperation();  
106 }
107
108 //=================================================================================
109 // name     : abortOperation
110 // purpose  :
111 //=================================================================================
112 void SMESHGUI_SelectionOp::abortOperation()
113 {
114   removeCustomFilters();
115   setSelectionMode( myOldSelectionMode );  
116   SMESHGUI_Operation::abortOperation();
117 }
118
119 //=================================================================================
120 // name     : selectionDone
121 // purpose  :
122 //=================================================================================
123 void SMESHGUI_SelectionOp::selectionDone()
124 {
125   if( !dlg() )
126     return;
127     
128   QStringList names, ids;
129   SalomeApp_Dialog::TypesList types;
130   selected( names, types, ids );
131   dlg()->selectObject( names, types, ids );
132 }
133
134 //=================================================================================
135 // name     : createFilter
136 // purpose  :
137 //=================================================================================
138 SUIT_SelectionFilter* SMESHGUI_SelectionOp::createFilter( const int ) const
139 {
140   return 0;
141 }
142
143 //=================================================================================
144 // name     : onActivateObject
145 // purpose  :
146 //=================================================================================
147 void SMESHGUI_SelectionOp::onActivateObject( int id )
148 {
149   SalomeApp_SelectionMgr* mgr = selectionMgr();
150   if( !mgr )
151     return;
152     
153   if( !myFilters.contains( id ) )
154     myFilters[ id ] = createFilter( id );
155
156   if( myFilters[ id ] )
157     mgr->installFilter( myFilters[ id ] );
158
159   selectionDone();
160 }
161
162 //=================================================================================
163 // name     : onDeactivateObject
164 // purpose  :
165 //=================================================================================
166 void SMESHGUI_SelectionOp::onDeactivateObject( int id )
167 {
168   SalomeApp_SelectionMgr* mgr = selectionMgr();
169   if( mgr && myFilters.contains( id ) && myFilters[ id ] )
170     mgr->removeFilter( myFilters[ id ] );
171 }
172
173 //=================================================================================
174 // name     : initDialog
175 // purpose  :
176 //=================================================================================
177 void SMESHGUI_SelectionOp::initDialog()
178 {
179   if( dlg() )
180   {
181     dlg()->clearSelection();
182     dlg()->deactivateAll();
183   }
184 }
185
186 //=================================================================================
187 // name     : initDialog
188 // purpose  :
189 //=================================================================================
190 void SMESHGUI_SelectionOp::onSelectionChanged( int )
191 {
192 }