Salome HOME
2436435d5326e7fe6f0b795e2898eb14f6e22e96
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SelectionIdsOp.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_SelectionIdsOp.cxx
23 //  Author : Alexander SOLOVYOV
24 //  Module : SMESH
25
26 #include <SMESHGUI_SelectionIdsOp.h>
27 #include <SMESHGUI_Utils.h>
28 #include <SMESHGUI_VTKUtils.h>
29 #include <SMESHGUI_MeshUtils.h>
30
31 #include <SalomeApp_SelectionMgr.h>
32
33 #include <SALOME_ListIO.hxx>
34 #include <SVTK_ViewModel.h>
35 #include <SVTK_Selector.h>
36
37 #include <SMDS_Mesh.hxx>
38 #include <SMESH_Actor.h>
39
40 #include <TColStd_MapOfInteger.hxx>
41
42 /*
43   Class       : SMESHGUI_SelectionIdsOp
44   Description : 
45 */
46
47 //=================================================================================
48 // name     : SMESHGUI_SelectionIdsOp
49 // purpose  : 
50 //=================================================================================
51 SMESHGUI_SelectionIdsOp::SMESHGUI_SelectionIdsOp( const Selection_Mode mode )
52 : SMESHGUI_SelectionOp( mode )
53 {
54 }
55
56 //=================================================================================
57 // name     : ~SMESHGUI_SelectionIdsOp
58 // purpose  :
59 //=================================================================================
60 SMESHGUI_SelectionIdsOp::~SMESHGUI_SelectionIdsOp()
61 {
62 }
63
64 //=================================================================================
65 // name     : startOperation
66 // purpose  :
67 //=================================================================================
68 void SMESHGUI_SelectionIdsOp::startOperation()
69 {
70   SMESHGUI_SelectionOp::startOperation();
71   if( dlg() )
72   {
73     disconnect( dlg(), SIGNAL( objectChanged( int, const QStringList& ) ),
74                 this,  SLOT( onTextChanged( int, const QStringList& ) ) );
75     connect( dlg(), SIGNAL( objectChanged( int, const QStringList& ) ),
76              this,  SLOT( onTextChanged( int, const QStringList& ) ) );    
77   }
78 }
79
80 //=================================================================================
81 // name     : commitOperation
82 // purpose  :
83 //=================================================================================
84 void SMESHGUI_SelectionIdsOp::commitOperation()
85 {
86   SMESHGUI_SelectionOp::commitOperation();
87   myMesh = SMESH::SMESH_Mesh::_nil();
88 }
89
90 //=================================================================================
91 // name     : abortOperation
92 // purpose  :
93 //=================================================================================
94 void SMESHGUI_SelectionIdsOp::abortOperation()
95 {
96   SMESHGUI_SelectionOp::abortOperation();
97   myMesh = SMESH::SMESH_Mesh::_nil();
98 }
99
100 //=================================================================================
101 // name     : selectionDone
102 // purpose  :
103 //=================================================================================
104 void SMESHGUI_SelectionIdsOp::selectionDone()
105 {
106   if( !dlg() )
107     return;
108     
109   // get selected mesh
110   SALOME_ListIO aList;
111   selectionMgr()->selectedObjects(aList,SVTK_Viewer::Type());
112
113   if( aList.Extent() != 1)
114   {
115     myMesh = SMESH::SMESH_Mesh::_nil();
116     dlg()->clearSelection();
117     return;
118   }
119
120   Handle(SALOME_InteractiveObject) anIO = aList.First();
121   myMesh = SMESH::GetMeshByIO(anIO);
122
123   QStringList names, ids; SalomeApp_Dialog::TypesList types;
124   selected( names, types, ids );  
125   dlg()->selectObject( names, types, ids );
126 }
127
128 //=================================================================================
129 // name     : mesh
130 // purpose  :
131 //=================================================================================
132 SMESH::SMESH_Mesh_var SMESHGUI_SelectionIdsOp::mesh() const
133 {
134   return myMesh;
135 }
136
137 //=================================================================================
138 // name     : actor
139 // purpose  :
140 //=================================================================================
141 SMESH_Actor* SMESHGUI_SelectionIdsOp::actor() const
142 {
143   return SMESH::FindActorByObject( myMesh.in() );
144 }
145
146 //=================================================================================
147 // name     : onTextChanged
148 // purpose  :
149 //=================================================================================
150 void SMESHGUI_SelectionIdsOp::onTextChanged( int, const QStringList& list )
151 {
152     if( !dlg() )
153       return;
154
155     TColStd_MapOfInteger newIndices;
156
157     SALOME_ListIO sel; selectionMgr()->selectedObjects( sel );
158     SMESH_Actor* anActor = actor();
159     if( sel.Extent()==0 || !anActor )
160       return;
161
162     SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
163
164     IdList ids; extractIds( list, ids, '\0' );
165     IdList::const_iterator anIt = ids.begin(),
166                            aLast = ids.end();
167     for( ; anIt!=aLast; anIt++ )
168       if( const SMDS_MeshNode * n = aMesh->FindNode( *anIt ) )
169         newIndices.Add( n->GetID() );
170
171     selector()->AddOrRemoveIndex( sel.First(), newIndices, false );
172     highlight( sel.First(), true, true );
173
174     QStringList names, _ids; SalomeApp_Dialog::TypesList types;
175     selected( names, types, _ids );
176     dlg()->selectObject( names, types, _ids, false );
177 }
178
179 //=================================================================================
180 // name     : selectedIds
181 // purpose  :
182 //=================================================================================
183 void SMESHGUI_SelectionIdsOp::selectedIds( const int id, IdList& list ) const
184 {
185   if( !dlg() )
186     return;
187     
188   QStringList ids; dlg()->selectedObject( id, ids );
189   extractIds( ids, list );
190 }
191
192 //=================================================================================
193 // name     : extractIds
194 // purpose  :
195 //=================================================================================
196 void SMESHGUI_SelectionIdsOp::extractIds( const QStringList& ids, IdList& list, const QChar idchar )
197 {
198   QStringList::const_iterator anIt = ids.begin(),
199                               aLast = ids.end();
200   QString id_str;
201   for( ; anIt!=aLast; anIt++ )
202   {
203     id_str = *anIt;
204     int pos = idchar=='\0' ? -1 : id_str.find( idchar );
205     int id = -1;
206     if( idchar=='\0' || pos>=0 )
207     {
208       id = id_str.mid( pos+1 ).toInt();
209       list.append( id );
210     }
211   }  
212 }
213
214 //=================================================================================
215 // name     : extractIds
216 // purpose  :
217 //=================================================================================
218 void SMESHGUI_SelectionIdsOp::extractIds( const QStringList& ids, IdList& list ) const
219 {
220   extractIds( ids, list, idChar() );
221 }